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

asked Mar 23, 2015 at 2:32

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,)
Pikamander2
8,4504 gold badges58 silver badges75 bronze badges
answered Mar 23, 2015 at 2:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.