【问题标题】:Inserting python variables into MySQL syntax problems将python变量插入MySQL语法问题
【发布时间】:2014-04-09 08:34:49
【问题描述】:

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

>>>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

【问题讨论】:

    标签: python mysql variables


    【解决方案1】:

    y 应该在一个元组中:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 2016-09-21
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多