Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda