I am using Python MySQLdb to insert data into a mysql database.
InsertList contains many rows. All are valid except for a few which violate database integrity rules.
If I run the code below, the command returns an error.
Cursor1.executemany(query, InsertList)
How can I force executemany() to insert the rows which are valid but ignore the few which are erroneous? The erroneous ones are caused by duplicate values in the new row. Do I have to use execute() one by one to insert the rows instead?
Thank you for your help.
Use the SQL command
INSERT IGNORE INTO
instead of plain
INSERT INTO
Here is a reference to the docs. Note that there is also INSERT REPLACE (to replace duplicates with the new values) and INSERT ... ON DUPLICATE KEY UPDATE for more control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With