将python变量插入MySQL语法问题

bro*_*kga 2 python mysql variables

有人能告诉我这个语法有什么问题吗?

>>>y = 14
>>> cursor.execute("INSERT INTO accounts VALUES ('Bobby', %s)", (y))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 477, in execute
    stmt = operation % self._process_params(params)
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 355, in      
_process_params
    "Failed processing format-parameters; %s" % err)
    mysql.connector.errors.ProgrammingError: Failed processing format-parameters; argument   
    2 to map() must support iteration
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 7

y 应该在一个元组内:

cursor.execute("INSERT INTO accounts VALUES ('Bobby', %s)", (y, ))
Run Code Online (Sandbox Code Playgroud)

仅供参考,(y)它不是一个元组 - 它是一个int括号,添加逗号(y,)使它成为一个元组里面有一个元素:

>>> y = 14
>>> type((y))
<type 'int'>
>>> type((y,))
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud)