0
votes

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?

1
shouldnt it be "select id, name, url from categories where", id_parent=%s - Maddy
@Maddy: No. Everything work fine if I set select_categories = ("select 'id', 'name', 'url' from categories where 'id_parent'=4") - TigerTV.ru
Your param should be sequence. A tuple of one element is (4,) but you have (4) which is a scalar. - Parfait
@Parfait: it works. Thanks!!! - TigerTV.ru

1 Answers

2
votes

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.