i have a chat bot django web application that take the user input and tries to find the matching input in the DB then retrieve the corresponding output.
This is a part of chatbot.py code:
# -*- coding: utf-8 -*-
import re,string,sys
import codecs
import types
import nltk
import csv
import pymysql
from nltk import pos_tag
from nltk.tokenize import regexp_tokenize
from nltk.tokenize import word_tokenize
from nltk.stem.isri import ISRIStemmer
from chatbotx1.arabic_const import *
from chatbotx1.normalize import *
from chatbotx1.stemming import *
from nltk.tag.stanford import StanfordPOSTagger
from chatbotx1.ar_ghalat import *
from chat.models import chat, user_info
# initialize the connection to the female_database
conn = pymysql.connect("***","***","***","***")
cursor = conn.cursor()
conn.text_factory = str
def run_conversation_male():
last_B =' '.join(chat.objects.values_list('chatbot_response', flat=True).latest('id'))
H = ' '.join(chat.objects.values_list('user_iput', flat=True).latest('id'))
New_H= ' '.join(PreProcess_text(H))
cursor.execute('SELECT respoce FROM Male_Conversation_Engine WHERE request REGEXP?',[New_H])
reply = cursor.fetchone()
if reply:
B8=reply[0]
new_data(H,B8)
ID = chat.objects.values_list('id', flat=True).latest('id')
chat.objects.filter(id=ID).update(chatbot_response=B8)
Before i was using sqlite3, but now after using Mysql i got this error message:
TypeError: not all arguments converted during string formatting
I have tried this solution:
openconnect = pymysql.connect(host='xxxx',port=3306,user='xxx',passwd='xxx',db='xxxx',charset='utf8')
and i got this error message:
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax;)
(PS: I am using Python3.5,Mysql, pymysql,Django10, and the text is in Arabic language)
Problem with syntax is that, cursor.execute don't know where to put New_H. You need %s to mark place for that variable.
Try to change your SQL to this one:
SELECT respoce FROM Male_Conversation_Engine WHERE request REGEXP %s