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

1 Answer 1

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,)
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.