Reputation:
Im not sure exactly whats happening here but it might have something to do with format in python. Running this causes an error.
x = '00000201000012EB'
sql = """ SELECT * FROM table WHERE id = {} """.format(x)
conn.execute(sql)
I get an error saying: syntax error near "EB"
however when i run the command like this:
sql = """ SELECT * FROM table WHERE id = '00000201000012EB' """
conn.execute(sql)
It works fine.
Is there something wrong with the way im formatting this sql statement?
Upvotes: 2
Views: 349
Reputation: 121494
Use the variable as an argument to execute()
:
cur.execute(""" SELECT * FROM my_table WHERE id = %s """, (x,))
If you are determined to use format()
, you should add single quotes around the placeholder:
sql = """ SELECT * FROM my_table WHERE id = '{}' """.format(x)
Upvotes: 2
Reputation:
Believe it or not it was fixed by adding more quotes to the string.
this finally worked.
x = '00000201000012EB'
sql = """ SELECT * FROM table WHERE id = {} """.format("'" + x + "'")
Since the sql statement required another set of quotes i just added them to ensure it was treated as its own string.
Upvotes: 0