Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
using string variable with REGEXP select pymysql query in python3

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)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!