|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | +#------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +#------------------------------------------------------------------------------ |
| 6 | +# BatchErrors.py |
| 7 | +# |
| 8 | +# Demonstrate the use of the Oracle Database 12.1 feature that allows |
| 9 | +# cursor.executemany() to complete successfully, even if errors take |
| 10 | +# place during the execution of one or more of the individual |
| 11 | +# executions. The parameter "batcherrors" must be set to True in the |
| 12 | +# call to cursor.executemany() after which cursor.getbatcherrors() can |
| 13 | +# be called, which will return a list of error objects. |
| 14 | +# |
| 15 | +# This script requires cx_Oracle 5.2 and higher. |
| 16 | +#------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import cx_Oracle |
| 21 | +import SampleEnv |
| 22 | + |
| 23 | +connection = cx_Oracle.connect(SampleEnv.GetMainConnectString()) |
| 24 | +cursor = connection.cursor() |
| 25 | + |
| 26 | +# define data to insert |
| 27 | +dataToInsert = [ |
| 28 | + (1016, 10, 'Child B of Parent 10'), |
| 29 | + (1017, 10, 'Child C of Parent 10'), |
| 30 | + (1018, 20, 'Child D of Parent 20'), |
| 31 | + (1018, 20, 'Child D of Parent 20'), # duplicate key |
| 32 | + (1019, 30, 'Child C of Parent 30'), |
| 33 | + (1020, 30, 'Child D of Parent 40'), |
| 34 | + (1021, 60, 'Child A of Parent 60'), # parent does not exist |
| 35 | + (1022, 40, 'Child F of Parent 40'), |
| 36 | +] |
| 37 | + |
| 38 | +# retrieve the number of rows in the table |
| 39 | +cursor.execute(""" |
| 40 | + select count(*) |
| 41 | + from ChildTable""") |
| 42 | +count, = cursor.fetchone() |
| 43 | +print("number of rows in child table:", int(count)) |
| 44 | +print("number of rows to insert:", len(dataToInsert)) |
| 45 | + |
| 46 | +# old method: executemany() with data errors results in stoppage after the |
| 47 | +# first error takes place; the row count is updated to show how many rows |
| 48 | +# actually succeeded |
| 49 | +try: |
| 50 | + cursor.executemany("insert into ChildTable values (:1, :2, :3)", |
| 51 | + dataToInsert) |
| 52 | +except cx_Oracle.DatabaseError as e: |
| 53 | + error, = e.args |
| 54 | + print("FAILED with error:", error.message) |
| 55 | + print("number of rows which succeeded:", cursor.rowcount) |
| 56 | + |
| 57 | +# demonstrate that the row count is accurate |
| 58 | +cursor.execute(""" |
| 59 | + select count(*) |
| 60 | + from ChildTable""") |
| 61 | +count, = cursor.fetchone() |
| 62 | +print("number of rows in child table after failed insert:", int(count)) |
| 63 | + |
| 64 | +# roll back so we can perform the same work using the new method |
| 65 | +connection.rollback() |
| 66 | + |
| 67 | +# new method: executemany() with batch errors enabled (and array DML row counts |
| 68 | +# also enabled) results in no immediate error being raised |
| 69 | +cursor.executemany("insert into ChildTable values (:1, :2, :3)", dataToInsert, |
| 70 | + batcherrors = True, arraydmlrowcounts = True) |
| 71 | + |
| 72 | +# where errors have taken place, the row count is 0; otherwise it is 1 |
| 73 | +rowCounts = cursor.getarraydmlrowcounts() |
| 74 | +print("Array DML row counts:", rowCounts) |
| 75 | + |
| 76 | +# display the errors that have taken place |
| 77 | +errors = cursor.getbatcherrors() |
| 78 | +print("number of errors which took place:", len(errors)) |
| 79 | +for error in errors: |
| 80 | + print("Error", error.message.rstrip(), "at row offset", error.offset) |
| 81 | + |
| 82 | +# demonstrate that all of the rows without errors have been successfully |
| 83 | +# inserted |
| 84 | +cursor.execute(""" |
| 85 | + select count(*) |
| 86 | + from ChildTable""") |
| 87 | +count, = cursor.fetchone() |
| 88 | +print("number of rows in child table after successful insert:", int(count)) |
0 commit comments