Tengo una aplicación web de chat bot django que toma la entrada del usuario e intenta encontrar la entrada coincidente en la base de datos y luego recupera la salida correspondiente.
Esta es una parte del código chatbot.py:
# -*- 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)Antes estaba usando sqlite3, pero ahora, después de usar Mysql, recibí este mensaje de error:
TypeError: not all arguments converted during string formattingHe probado esta solución:
openconnect = pymysql.connect(host='xxxx',port=3306,user='xxx',passwd='xxx',db='xxxx',charset='utf8')y recibí este mensaje de error:
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax;)(PD: estoy usando Python3.5, Mysql, pymysql, Django10, y el texto está en idioma árabe)
El problema con la sintaxis es que cursor.execute no sabe dónde colocar New_H . Necesita %s para marcar el lugar de esa variable. Intenta cambiar tu SQL a este:
SELECT respoce FROM Male_Conversation_Engine WHERE request REGEXP %s