在python3中使用字符串变量REGEXP select pymysql query

2024-10-04 01:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个聊天机器人django web应用程序,它接受用户输入,并尝试在数据库中找到匹配的输入,然后检索相应的输出。在

这是聊天机器人.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)

在我使用sqlite3之前,但是现在在使用Mysql之后,我收到了以下错误消息:

^{pr2}$

我尝试过这个解决方案:

  openconnect = pymysql.connect(host='xxxx',port=3306,user='xxx',passwd='xxx',db='xxxx',charset='utf8')

我收到了一条错误信息:

  pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax;) 

(注:我使用的是Python3.5、Mysql、pymysql、Django10,文本为阿拉伯语)


Tags: fromimportidobjectschatconncursorlist
1条回答
网友
1楼 · 发布于 2024-10-04 01:33:25

语法的问题是,cursor.execute不知道把New_H放在哪里。您需要%s来标记该变量的位置。 请尝试将您的SQL更改为:

SELECT respoce FROM Male_Conversation_Engine WHERE request REGEXP %s

相关问题 更多 >