Python Forum
Problem with quotes on MySQL request
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with quotes on MySQL request
#1
Hello World,

i've been searching for a while but i've found nothing so i post it here.

My problem is that I want to create a function that search for an id on a MySQL database which the row contains a certain word.

Here is my programm:

import MySQLdb
    
def rechercheMySQL(): 
    table=raw_input("Nom de la TABLE à explorer:")
    mot=raw_input("Mot à rechercher:")
    filtre=raw_input("Filtrer dans:")
    tri=raw_input("Trier par:")
    mydb = MySQLdb.connect(host='localhost', user='root', passwd='eisti0001', db='DATAtourisme')
    cursor = mydb.cursor()
    SQL="SELECT %s FROM %s WHERE %s LIKE %s"
    cursor.execute(SQL, (tri, table, filtre, mot))        
    
if __name__ == "__main__":
    rechercheMySQL()
and it returns:

python RechercheMYSQL.py
Nom de la TABLE à explorer:Theatre
Mot à rechercher:cerises
Filtrer dans:commentaire
Trier par:id
Traceback (most recent call last):
  File "RechercheMYSQL.py", line 21, in <module>
    rechercheMySQL()
  File "RechercheMYSQL.py", line 18, in rechercheMySQL
    cursor.execute(SQL, (tri, table, filtre, mot))        
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Theatre' WHERE 'commentaire' LIKE 'cerises'' at line 1")
Basically it put quotes in the SQL syntax that i do not want and i don't know how to get rid of it. Do you have any idea?

Sorry for my English or if i did something wrong, first time posting on a forum Big Grin
Reply


Messages In This Thread
Problem with quotes on MySQL request - by PierreSoulier - Jun-19-2018, 08:43 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020