我正在使用Python3.x的MySQL连接器,并通过如下的简单代码解决了这个问题:
rest_cursor.execute("SELECT * FROM restaurant WHERE r_id=%s",(r_id))
其中r_id是整数。这将导致抛出异常: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
此格式似乎与python-MySQL示例页面中的示例相匹配:http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html。
我做错了什么?谢谢
发布于 2015-03-23 10:38:19
要创建单项元组,必须在结束括号前包含一个逗号:
rest_cursor.execute("SELECT * FROM restaurant WHERE r_id=%s", (r_id,))否则,Python会认为您只是为了分组而将一个值包含在圆括号中。
再举一个例子,注意Python计算这两个值的方式的不同:
(1) → 1
(1,) → (1,)https://stackoverflow.com/questions/29202471
复制相似问题