0

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?

4
  • shouldnt it be "select id, name, url from categories where", id_parent=%s Commented Mar 2, 2018 at 18:33
  • @Maddy: No. Everything work fine if I set select_categories = ("select 'id', 'name', 'url' from categories where 'id_parent'=4") Commented Mar 2, 2018 at 18:35
  • 1
    Your param should be sequence. A tuple of one element is (4,) but you have (4) which is a scalar. Commented Mar 2, 2018 at 18:38
  • @Parfait: it works. Thanks!!! Commented Mar 2, 2018 at 18:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.