How to On Duplicate Key Update

一曲冷凌霜 提交于 2019-12-25 03:27:24

问题


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.

My unique column 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.


回答1:


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]))

Check also Insert on duplicate.



来源:https://stackoverflow.com/questions/25322880/how-to-on-duplicate-key-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!