Python mysql.connector更新错误代码1064

时间:2015-11-03 06:36:52

标签: python mysql-connector executemany

我正在尝试插入一组int。我使用上面完全相同的代码serval行来更新" processed = 1"从0开始。代码如下:

cursor.executemany('''UPDATE %s SET flag = "bad" WHERE id = %%s''' % seed_table, (bad_list))

我得到的错误,我打印出列表,我也检查确保那些内容是否有整数:

bad_list: [61, 63, 68, 69]
Error code: 1064
SQLSTATE value: 42000
Error message: 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

当我成功地使用这个确切的语法时,为什么这不起作用的任何帮助将非常感谢。

更新:我打印出我想要执行的内容:

UPDATE table_name SET flag = "bad" WHERE id = %s [231, 233, 234, 235, 236, 237, 239, 240]

这不是出于某种原因而工作,下面是一个正常工作的例子:

INSERT INTO table_name (col1, col2, col3, col4, col5, col6, col7,col8) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) [(u'jungkook covers', 1.0, 1.0, 1.0, 18.0, 'all', '2015-08-02', '2015-10-30'), (u'songs for dancing', 2.0, 2.0, 1.0, 9.0, 'all', '2015-08-02', '2015-10-30'), (u'songs', 4.0, 2.0, 0.5, 6.0, 'all', '2015-08-02', '2015-10-30'), (u'music', 1.0, 1.0, 1.0, 7.0, 'all', '2015-08-02', '2015-10-30'), (u'songs about yourself', 3.0, 1.0, 0.3333333333333333, 10.0, 'all', '2015-08-02', '2015-10-30')]

1 个答案:

答案 0 :(得分:1)

要在python中使用%s或{position}格式化字符串。

例如'first {0}, second {1} ...'.format(val0, val1, ...)

要更新SQL中的许多行,请使用id in (id1, id2, ...)