Python and mySQLdb error: OperationalError: (1054, "Unknown column in where where where section")

Hi everyone, I get an error

OperationalError: (1054, "Unknown column" XX "in the" where "section)

Where XX is the CLASS value in the following code

conn = MySQLdb.connect(host = "localhost",user = "user", passwd = "pass",db = "dbase")
cursor = conn.cursor()
cursor.execute("""SELECT * FROM %s WHERE course =%s AND sec = %s""" % (str(DEPT),str(CLASS),str(SEC),))

The fact is that I get this error only with certain values, namely when CLASS contains a letter. I have a table configured as varchar if this helps

Thank!

+3
source share
2 answers

"string injection" SQL, , , str(DEPT) , , . API- Python - "SQL-", . ( ).

MySQLdb %s , ( , PEP8, , ;):

conn = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="dbase")
cursor = conn.cursor()
q = 'SELECT * FROM %s WHERE course=%%s AND sec = %%s""" % (DEPT,)
cursor.execute(q, (CLASS, SEC))

%% , q, % , q %s, execute CLASS SEC. str ..

, Python 2.6 , format %, "" % ", . , 2.5 ( Python, ).

+5

:

course=%s

, :

course='%s'
+1

Source: https://habr.com/ru/post/1759213/


All Articles