如何进行重复密钥更新 [英] How to On Duplicate Key Update

查看:99
本文介绍了如何进行重复密钥更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在我的Python脚本中执行的查询,但是当它插入数据库并找到我唯一列的重复项时,它将导致错误并停止.我知道我需要使用重复密钥更新",但请注意确定如何正确添加它.

I have this query that is executed in my Python script but when its inserting into the database and finds a duplicate of my unique column it causes it to error and stops. I know I need to use On Duplicate Key Update but I'm note sure how to properly add this.

我独特的第2列.

cur.execute("""INSERT INTO logs (1,2,3) VALUES (%s,%s,%s) """,(line[0], line[1], line[2]))

如果有重复项可以更新该行/条目.

If there is a duplicate to have it update that row/entry.

推荐答案

当我正确理解您的意思后,您正在寻找的是这样的东西:

When I understand you correctly, what you are looking for is this:

cur.execute(""" INSERT INTO logs (1, 2, 3) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE 1=%s, 3=%s """, (line[0], line[1], line[2], line[0], line[2]))

还要插入重复项.

这篇关于如何进行重复密钥更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆