将python变量插入MySQL语法问题

时间:2014-04-09 00:31:31

标签: 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

1 个答案:

答案 0 :(得分:7)

y应该在元组中:

cursor.execute("INSERT INTO accounts VALUES ('Bobby', %s)", (y, ))

仅供参考,(y)不是元组 - 它是括号中的int,添加逗号(y,)使其成为一个元组,其中包含一个元素:

>>> y = 14
>>> type((y))
<type 'int'>
>>> type((y,))
<type 'tuple'>