|
40 | 40 | if not sample_env.get_is_thin(): |
41 | 41 | oracledb.init_oracle_client(lib_dir=sample_env.get_oracle_client()) |
42 | 42 |
|
43 | | -connection = oracledb.connect(sample_env.get_main_connect_string()) |
44 | | -cursor = connection.cursor() |
| 43 | +connection = oracledb.connect(user=sample_env.get_main_user(), |
| 44 | + password=sample_env.get_main_password(), |
| 45 | + dsn=sample_env.get_connect_string()) |
45 | 46 |
|
46 | | -# define data to insert |
47 | | -data_to_insert = [ |
48 | | - (1016, 10, 'Child B of Parent 10'), |
49 | | - (1017, 10, 'Child C of Parent 10'), |
50 | | - (1018, 20, 'Child D of Parent 20'), |
51 | | - (1018, 20, 'Child D of Parent 20'), # duplicate key |
52 | | - (1019, 30, 'Child C of Parent 30'), |
53 | | - (1020, 30, 'Child D of Parent 40'), |
54 | | - (1021, 60, 'Child A of Parent 60'), # parent does not exist |
55 | | - (1022, 40, 'Child F of Parent 40'), |
56 | | -] |
| 47 | +with connection.cursor() as cursor: |
57 | 48 |
|
58 | | -# retrieve the number of rows in the table |
59 | | -cursor.execute(""" |
60 | | - select count(*) |
61 | | - from ChildTable""") |
62 | | -count, = cursor.fetchone() |
63 | | -print("number of rows in child table:", int(count)) |
64 | | -print("number of rows to insert:", len(data_to_insert)) |
| 49 | + # retrieve the number of rows in the table |
| 50 | + cursor.execute(""" |
| 51 | + select count(*) |
| 52 | + from ChildTable""") |
| 53 | + count, = cursor.fetchone() |
| 54 | + print("Number of rows in child table:", int(count)) |
65 | 55 |
|
66 | | -# old method: executemany() with data errors results in stoppage after the |
67 | | -# first error takes place; the row count is updated to show how many rows |
68 | | -# actually succeeded |
69 | | -try: |
70 | | - cursor.executemany("insert into ChildTable values (:1, :2, :3)", |
71 | | - data_to_insert) |
72 | | -except oracledb.DatabaseError as e: |
73 | | - error, = e.args |
74 | | - print("FAILED with error:", error.message) |
75 | | - print("number of rows which succeeded:", cursor.rowcount) |
| 56 | + # define data to insert |
| 57 | + data_to_insert = [ |
| 58 | + (1016, 10, 'Child B of Parent 10'), |
| 59 | + (1017, 10, 'Child C of Parent 10'), |
| 60 | + (1018, 20, 'Child D of Parent 20'), |
| 61 | + (1018, 20, 'Child D of Parent 20'), # duplicate key |
| 62 | + (1019, 30, 'Child C of Parent 30'), |
| 63 | + (1020, 30, 'Child D of Parent 40'), |
| 64 | + (1021, 60, 'Child A of Parent 60'), # parent does not exist |
| 65 | + (1022, 40, 'Child F of Parent 40'), |
| 66 | + ] |
| 67 | + print("Number of rows to insert:", len(data_to_insert)) |
| 68 | + |
| 69 | + # old method: executemany() with data errors results in stoppage after the |
| 70 | + # first error takes place; the row count is updated to show how many rows |
| 71 | + # actually succeeded |
| 72 | + try: |
| 73 | + cursor.executemany("insert into ChildTable values (:1, :2, :3)", |
| 74 | + data_to_insert) |
| 75 | + except oracledb.DatabaseError as e: |
| 76 | + error, = e.args |
| 77 | + print("Failure with error:", error.message) |
| 78 | + print("Number of rows successfully inserted:", cursor.rowcount) |
76 | 79 |
|
77 | | -# demonstrate that the row count is accurate |
78 | | -cursor.execute(""" |
79 | | - select count(*) |
80 | | - from ChildTable""") |
81 | | -count, = cursor.fetchone() |
82 | | -print("number of rows in child table after failed insert:", int(count)) |
| 80 | + # demonstrate that the row count is accurate |
| 81 | + cursor.execute(""" |
| 82 | + select count(*) |
| 83 | + from ChildTable""") |
| 84 | + count, = cursor.fetchone() |
| 85 | + print("Number of rows in child table after failed insert:", int(count)) |
83 | 86 |
|
84 | | -# roll back so we can perform the same work using the new method |
85 | | -connection.rollback() |
| 87 | + # roll back so we can perform the same work using the new method |
| 88 | + connection.rollback() |
86 | 89 |
|
87 | | -# new method: executemany() with batch errors enabled (and array DML row counts |
88 | | -# also enabled) results in no immediate error being raised |
89 | | -cursor.executemany("insert into ChildTable values (:1, :2, :3)", |
90 | | - data_to_insert, batcherrors=True, arraydmlrowcounts=True) |
| 90 | + # new method: executemany() with batch errors enabled (and array DML row |
| 91 | + # counts also enabled) results in no immediate error being raised |
| 92 | + cursor.executemany("insert into ChildTable values (:1, :2, :3)", |
| 93 | + data_to_insert, batcherrors=True, |
| 94 | + arraydmlrowcounts=True) |
91 | 95 |
|
92 | | -# where errors have taken place, the row count is 0; otherwise it is 1 |
93 | | -row_counts = cursor.getarraydmlrowcounts() |
94 | | -print("Array DML row counts:", row_counts) |
| 96 | + # display the errors that have taken place |
| 97 | + errors = cursor.getbatcherrors() |
| 98 | + print("Number of rows with bad values:", len(errors)) |
| 99 | + for error in errors: |
| 100 | + print("Error", error.message.rstrip(), "at row offset", error.offset) |
95 | 101 |
|
96 | | -# display the errors that have taken place |
97 | | -errors = cursor.getbatcherrors() |
98 | | -print("number of errors which took place:", len(errors)) |
99 | | -for error in errors: |
100 | | - print("Error", error.message.rstrip(), "at row offset", error.offset) |
| 102 | + # arraydmlrowcounts also shows rows with invalid data: they have a row |
| 103 | + # count of 0; otherwise 1 is shown |
| 104 | + row_counts = cursor.getarraydmlrowcounts() |
| 105 | + print("Array DML row counts:", row_counts) |
101 | 106 |
|
102 | | -# demonstrate that all of the rows without errors have been successfully |
103 | | -# inserted |
104 | | -cursor.execute(""" |
105 | | - select count(*) |
106 | | - from ChildTable""") |
107 | | -count, = cursor.fetchone() |
108 | | -print("number of rows in child table after successful insert:", int(count)) |
| 107 | + # demonstrate that all of the rows without errors have been successfully |
| 108 | + # inserted |
| 109 | + cursor.execute(""" |
| 110 | + select count(*) |
| 111 | + from ChildTable""") |
| 112 | + count, = cursor.fetchone() |
| 113 | + print("Number of rows in child table after insert with batcherrors " |
| 114 | + "enabled:", int(count)) |
0 commit comments