没有足够的参数用于bool和list的格式字符串

时间:2017-03-29 11:17:39

标签: python mysql

我是python的新手并尝试使用列表上的IN更新所有行的一个列值(true / false)(列表中提供的ID)。

我的查询是这样的:

qry = "Update table Set "\
      "col1 = %s "\
      "where col2 in ( " + ",".join( "%s" for _ in lstIds)
qry += ")"
cursor.execute(qry,(col1Value, lstIds))

但我收到错误

  

格式字符串

的参数不够

我尝试了不同的解决方案但无法解决我的问题。 当我以这种方式更改查询时,it works fine

qry = "Update table Set "\
  "col1 = 1 "\
  "where col2 in ( " + ",".join( "%s" for _ in lstIds)
qry += ")"
cursor.execute(qry,( lstIds))

所以我猜问题是第一个参数值,但我不知道如何解决这个问题。

任何形式的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:3)

您需要将lstIds中的每个值都包含在一个单独的参数中:

cursor.execute(qry, (col1Value,) + lstIds)

假设lstIds是元组,或

cursor.execute(qry, [col1Value] + lstIds)

如果是列表。

或者您可以使用Python 3.5及更新版本中的*表示法将序列组合到一个列表中:

cursor.execute(qry, [col1Value, *lstIds])