|
| 1 | +#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/ |
| 2 | +# section titled "Resetting the data" |
| 3 | +#The following resets the data for use with the update section |
| 4 | +#For both tables: |
| 5 | +# Table data is removed. |
| 6 | +# The identity column is set to start with the id after the starting data. |
| 7 | +# Using the executemany function an array of starting data is inserted into the table. |
| 8 | + |
| 9 | +import cx_Oracle |
| 10 | +import os |
| 11 | +connectString = os.getenv('db_connect') |
| 12 | +con = cx_Oracle.connect(connectString) |
| 13 | +cur = con.cursor() |
| 14 | + |
| 15 | +# Delete rows |
| 16 | +statement = 'delete from lcs_pets' |
| 17 | +cur.execute(statement) |
| 18 | + |
| 19 | +# Reset Identity Coulmn |
| 20 | +statement = 'alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 8)' |
| 21 | +cur.execute(statement) |
| 22 | + |
| 23 | +# Delete rows |
| 24 | +statement = 'delete from lcs_people' |
| 25 | +cur.execute(statement) |
| 26 | + |
| 27 | +# Reset Identity Coulmn |
| 28 | +statement = 'alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 8)' |
| 29 | +cur.execute(statement) |
| 30 | + |
| 31 | +# Insert default rows |
| 32 | +rows = [(1, 'Bob', 35, 'I like dogs'), |
| 33 | + (2, 'Kim', 27, 'I like birds'), |
| 34 | + (3, 'Cheryl', 23, 'I like horses'), |
| 35 | + (4, 'Bob', 27, 'I like rabbits'), |
| 36 | + (5, 'Stacey', 45, 'I like snakes'), |
| 37 | + (6, 'Pete', 23, 'I like cats'), |
| 38 | + (7, 'Pat', 36, 'I like dogs')] |
| 39 | +cur.bindarraysize = 2 |
| 40 | +cur.setinputsizes(int, 20, int, 100) |
| 41 | +cur.executemany("insert into lcs_people(id, name, age, notes) values (:1, :2, :3, :4)", rows) |
| 42 | +con.commit() |
| 43 | + |
| 44 | +# Insert default rows |
| 45 | +rows = [(1, 'Duke', 1, 'dog'), |
| 46 | + (2, 'Dragon', 2, 'bird'), |
| 47 | + (3, 'Sneaky', 5, 'snake'), |
| 48 | + (4, 'Red', 2, 'bird'), |
| 49 | + (5, 'Red', 3, 'horse'), |
| 50 | + (6, 'Buster', 1, 'dog'), |
| 51 | + (7, 'Fido', 7, 'cat')] |
| 52 | +cur.bindarraysize = 2 |
| 53 | +cur.setinputsizes(int, 20, int, 100) |
| 54 | +cur.executemany("insert into lcs_pets (id, name, owner, type) values (:1, :2, :3, :4)", rows) |
| 55 | +con.commit() |
| 56 | + |
| 57 | +cur.close() |
0 commit comments