I've got an MySQL syntax error when I run the following code:
cnx = mysql.connector.connect(**connection)
cursor = cnx.cursor()
select_categories = ("select `id`, `name`, `url` from categories where `id_parent`=%s")
cursor.execute( select_categories, (4) )
Error:
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s' at line 1
How to fix it?
asked Mar 2, 2018 at 18:28
TigerTV.ru
1,0862 gold badges17 silver badges34 bronze badges
1 Answer 1
The problem is that python interprets (4) as 4 and the parameter needs to be an iterable. Try to update to this:
cursor.execute( select_categories, (4, ) )
The comma after the 4 will force python to interpret this as a tuple.
answered Mar 2, 2018 at 18:39
abigperson
5,3823 gold badges25 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
"select id, name, url from categories where", id_parent=%sselect_categories = ("select 'id', 'name', 'url' from categories where 'id_parent'=4")(4,)but you have(4)which is a scalar.