所以我有这些Python代码:
id=10001
ip="10.0.0.1"
up=[80, 5555]
down=0
sql = "INSERT INTO ip(id, ip, open, closed) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE open=VALUES('%s');" % (id, ip, up, down, up)
当我运行它时,它出现错误:
(1064, "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 ''[80, 5555]')' at line 1")
注意:当我删除时:
ON DUPLICATE KEY UPDATE open=VALUES('%s');
运行得很好
关于可能出现什么问题的任何想法吗?
最佳答案
删除 VALUES()
中的单引号。参数是列名:
INSERT INTO ip(id, ip, open, closed)
VALUES (%d, '%s', '%s', '%s')
ON DUPLICATE KEY UPDATE open = VALUES(%s);
但是,我希望正确的查询是:
INSERT INTO ip(id, ip, open, closed)
VALUES (%d, '%s', '%s', '%s')
ON DUPLICATE KEY UPDATE open = VALUES(open);
注意:open
是一个关键字,尽管它不是保留字。在我看来,这对于列名来说是一个糟糕的选择。
关于python mysql 重复 key 更新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644137/