Question

What I am trying to do is insert a bunch of rows using cx_Oracle and if there is an error (i.e. constraint violation) log the bad row.

Here is the sudo code for how I would do it using execute, however this doesn't seem very efficient.

for row in rows:
    try:
        cursor.execute("sql", row)
    except InsertError:
        badRecordCSVFile.write(row)

I read the docs and at first it seemed like executemany was what I was looking for. However, after reading this thread it would seem that executemany simply processes queries until it fails (not what I want).

So my question is what is the best way to insert rows and log the ones that fail?

Was it helpful?

Solution

If you are using SQL parameters for the insertion statement, this is efficient enough; the bottleneck will be in the network communication with Oracle.

By using SQL parameters, Oracle is given a chance to prepare a query plan, then reuse it on each and every insert.

If you plan to roll back the transaction when an insertion fails, however, you may as well use .executemany().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top