1

I'm using the MySQL connector for python 3.x and am getting this issue with simple code like:

rest_cursor.execute("SELECT * FROM restaurant WHERE r_id=%s",(r_id))

where r_id is an integer. This results in an exception being thrown: 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

This format seems to match the example from the python-MySQL example page: http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html.

What am I doing wrong? Thanks

user2441938
  • 145
  • 9

1 Answers1

7

To create a single-item tuple, you have to include a comma before the closing parenthesis:

rest_cursor.execute("SELECT * FROM restaurant WHERE r_id=%s", (r_id,))

Otherwise Python thinks that you're just enclosing a value in parentheses for the sake of grouping.

To give another example, note the difference in how Python evaluates these two values:

(1) → 1
(1,) → (1,)
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
Klaus D.
  • 13,874
  • 5
  • 41
  • 48