Python MySQLdb / MySQL INSERT IGNORE & Checking if Ignored

只愿长相守 提交于 2019-12-02 11:16:34

Naturally, a final search after I post the question yields the result.

mysql - after insert ignore get primary key

However, this still requires a second trip to the database. I would love to see if there's a clean pythonic way to do this with a single query.

query = "INSERT IGNORE ..."
cursor.execute(query)

# Last row was ignored
if cursor.lastrowid == 0:

This does an INSERT IGNORE query and if the insert is ignored (duplicate), the lastrowid will be 0.

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