Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) VALUES(%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" ?

like image 674
Lucas Kauffman Avatar asked Dec 09 '22 07:12

Lucas Kauffman


1 Answers

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))
like image 51
unutbu Avatar answered Dec 11 '22 20:12

unutbu