使用带有REGEXP的字符串变量在python3中选择pymysql查询

时间:2017-04-25 19:57:05

标签: python mysql django

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

这是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)

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

  TypeError: not all arguments converted during string formatting

我尝试过这个解决方案:

  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;) 

(PS:我使用的是Python3.5,Mysql,pymysql,Django10,文字是阿拉伯语)

1 个答案:

答案 0 :(得分:0)

语法问题在于,DAY days_07 days_14 days_30 2017-04-25 2087 4004 9700 2017-04-24 1947 4165 9611 2017-04-23 1666 4066 9599 2017-04-22 2121 4820 10014 2017-04-21 2885 5421 10192 不知道放置cursor.execute的位置。您需要%s来标记该变量的位置。 尝试将SQL更改为:

New_H