pythonoracle-databasecx-oracle

Python cx_Oracle executemany() function how to handle exception if one of the records fail?


I am using cx_oracle executemany function for inserting records in bulk. Let us say there are 10 records to be inserted, but one record is violating the unique key constraint. How can I make executemany to still persist 9 records leaving behind the 1 record with exception?


Solution

  • You can use the batcherrors flag: executemany(sql, data, batcherrors=True). This will continue processing the rows even if there are some errors and the errors can be examined later. You can find more information with an example here: https://cx-oracle.readthedocs.io/en/latest/user_guide/batch_statement.html#handling-data-errors.