开发者

Python MySQLdb unique records, ignore errors

I'm trying to insert records to my db from an array :

 for string in self.FinalMailsArray:
            c.execute("""INSERT INTO table (email) VALUE开发者_开发知识库S(%s) """,(string))

The problem is, that I want the field email to be unique, so I enabled that in the DB. When I start inserting, I get errors for duplicate entry value.

Is there a way where I can say, "if there is a duplicate error thrown, just go to the next string in the array" ?


INSERT IGNORE will ignore inserts that would otherwise conflict with a unique key:

for string in self.FinalMailsArray:
    c.execute("""INSERT IGNORE INTO table (email) VALUES(%s) """,(string))


You could use try/except.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜