【发布时间】:2019-05-09 13:24:40
【问题描述】:
我在 Python3 中使用 MySQLdb 来执行一些查询。其中一个我有一个user_ids 列表,并希望根据它们从我的用户表中删除。
当我执行查询时,我收到来自 MySQLdb 的警告:
Warning: (1292, "Truncated incorrect DOUBLE value: '108, 114, 109, 115'")
试图执行的查询是:
DELETE FROM user
WHERE id IN (108, 114, 109, 115);
我的代码如下所示:
cur.execute(get_users)
users = cur.fetchall()
users = [item[0] for item in users]
users = ", ".join(str(user) for user in users)
cur.execute(delete_query, [users])
我也尝试执行以下操作,它不会导致任何警告但是检查我的数据库我仍然可以看到这些用户存在:
users = ", ".join("\'{}\'".format(user) for user in users)
以下内容可以完美运行,但并不安全: cur.execute(delete_query.format(users))
是的,我正在我的代码中执行db.commit()。
【问题讨论】: