Skip to content

Commit 35dc0db

Browse files
committed
added CRUD examples
1 parent 6babf28 commit 35dc0db

31 files changed

+949
-0
lines changed

python/CRUD-examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The scripts in this directory follow the [BASIC CRUD OPERATIONS USING CX_ORACLE](https://learncodeshare.net/2015/06/02/basic-crud-operations-using-cx_oracle/) tutorial found on [LearnCodeShare.net](LearnCodeShare.net).
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#code Sample from the tutorial at https://learncodeshare.net/2015/06/02/basic-crud-operations-using-cx_oracle/
2+
#The following creates and populates the tables used for the tutorial
3+
4+
CREATE TABLE lcs_people (
5+
id NUMBER GENERATED BY DEFAULT AS identity,
6+
name VARCHAR2(20),
7+
age NUMBER,
8+
notes VARCHAR2(100)
9+
)
10+
/
11+
12+
ALTER TABLE LCS_PEOPLE
13+
ADD CONSTRAINT PK_LCS_PEOPLE PRIMARY KEY ("ID")
14+
/
15+
16+
CREATE TABLE LCS_PETS (
17+
id NUMBER GENERATED BY DEFAULT AS IDENTITY,
18+
name VARCHAR2(20),
19+
owner NUMBER,
20+
type VARCHAR2(100)
21+
)
22+
/
23+
24+
ALTER TABLE LCS_PETS ADD CONSTRAINT PK_LCS_PETS PRIMARY KEY ("ID")
25+
/
26+
27+
ALTER TABLE LCS_PETS ADD CONSTRAINT FK_LCS_PETS_OWNER FOREIGN KEY ("OWNER") REFERENCES "LCS_PEOPLE" ("ID")
28+
/
29+
30+
INSERT INTO lcs_people (name, age, notes)
31+
VALUES ('Bob', 35, 'I like dogs')
32+
/
33+
34+
INSERT INTO lcs_people (name, age, notes)
35+
VALUES ('Kim', 27, 'I like birds')
36+
/
37+
38+
INSERT INTO lcs_pets (name, owner, type)
39+
VALUES ('Duke', 1, 'dog')
40+
/
41+
42+
INSERT INTO lcs_pets (name, owner, type)
43+
VALUES ('Pepe', 2, 'bird')
44+
/
45+
46+
COMMIT
47+
/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/
2+
# section titled "Extra Fun 1"
3+
#Using the base template, the example code executes a simple delete using named bind variables.
4+
5+
import cx_Oracle
6+
import os
7+
connectString = os.getenv('db_connect')
8+
con = cx_Oracle.connect(connectString)
9+
10+
def get_all_rows(label, data_type='people'):
11+
# Query all rows
12+
cur = con.cursor()
13+
if (data_type == 'pets'):
14+
statement = 'select id, name, owner, type from lcs_pets order by owner, id'
15+
else:
16+
statement = 'select id, name, age, notes from lcs_people order by id'
17+
cur.execute(statement)
18+
res = cur.fetchall()
19+
print(label + ': ')
20+
print (res)
21+
print(' ')
22+
cur.close()
23+
24+
get_all_rows('Original Data', 'pets')
25+
26+
cur = con.cursor()
27+
statement = 'delete from lcs_pets where type = :type'
28+
cur.execute(statement, {'type':'bird'})
29+
con.commit()
30+
31+
get_all_rows('New Data', 'pets')
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/
2+
# section titled "Extra Fun 2"
3+
#Using the base template, the example code executes two simple deletes using named bind variables.
4+
# The child records are removed, followed by the parent record.
5+
6+
import cx_Oracle
7+
import os
8+
connectString = os.getenv('db_connect')
9+
con = cx_Oracle.connect(connectString)
10+
11+
def get_all_rows(label, data_type='people'):
12+
# Query all rows
13+
cur = con.cursor()
14+
if (data_type == 'pets'):
15+
statement = 'select id, name, owner, type from lcs_pets order by owner, id'
16+
else:
17+
statement = 'select id, name, age, notes from lcs_people order by id'
18+
cur.execute(statement)
19+
res = cur.fetchall()
20+
print(label + ': ')
21+
print (res)
22+
print(' ')
23+
cur.close()
24+
25+
get_all_rows('Original People Data', 'people')
26+
get_all_rows('Original Pet Data', 'pets')
27+
28+
cur = con.cursor()
29+
30+
statement = 'delete from lcs_pets where owner = :owner'
31+
cur.execute(statement, {'owner':5})
32+
33+
statement = 'delete from lcs_people where id = :id'
34+
cur.execute(statement, {'id':5})
35+
con.commit()
36+
37+
get_all_rows('New People Data', 'people')
38+
get_all_rows('New Pet Data', 'pets')
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/
2+
# section titled "Deleting records referenced by Foreign Keys" 1st example
3+
#Using the base template, the example code executes a simple delete using named bind variables.
4+
# When following the tutorial with default data this section intentionally throws an error
5+
# to demonstrate foreign key functionality.
6+
7+
import cx_Oracle
8+
import os
9+
connectString = os.getenv('db_connect')
10+
con = cx_Oracle.connect(connectString)
11+
12+
def get_all_rows(label, data_type='people'):
13+
# Query all rows
14+
cur = con.cursor()
15+
if (data_type == 'pets'):
16+
statement = 'select id, name, owner, type from lcs_pets order by owner, id'
17+
else:
18+
statement = 'select id, name, age, notes from lcs_people order by id'
19+
cur.execute(statement)
20+
res = cur.fetchall()
21+
print(label + ': ')
22+
print (res)
23+
print(' ')
24+
cur.close()
25+
26+
get_all_rows('Original People Data', 'people')
27+
get_all_rows('Original Pet Data', 'pets')
28+
29+
cur = con.cursor()
30+
statement = 'delete from lcs_people where id = :id'
31+
cur.execute(statement, {'id':1})
32+
con.commit()
33+
34+
get_all_rows('New People Data', 'people')
35+
get_all_rows('New Pet Data', 'pets')
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/
2+
# section titled "Deleting records referenced by Foreign Keys" 2nd example
3+
#Using the base template, the example code executes two simple deletes using named bind variables.
4+
# The child records are removed, followed by the parent record.
5+
6+
import cx_Oracle
7+
import os
8+
connectString = os.getenv('db_connect')
9+
con = cx_Oracle.connect(connectString)
10+
11+
def get_all_rows(label, data_type='people'):
12+
# Query all rows
13+
cur = con.cursor()
14+
if (data_type == 'pets'):
15+
statement = 'select id, name, owner, type from lcs_pets order by owner, id'
16+
else:
17+
statement = 'select id, name, age, notes from lcs_people order by id'
18+
cur.execute(statement)
19+
res = cur.fetchall()
20+
print(label + ': ')
21+
print (res)
22+
print(' ')
23+
cur.close()
24+
25+
get_all_rows('Original People Data', 'people')
26+
get_all_rows('Original Pet Data', 'pets')
27+
28+
cur = con.cursor()
29+
30+
statement = 'update lcs_pets set owner = :1 where owner = :2'
31+
cur.execute(statement, (2, 1))
32+
33+
statement = 'delete from lcs_people where id = :id'
34+
cur.execute(statement, {'id':1})
35+
con.commit()
36+
37+
get_all_rows('New People Data', 'people')
38+
get_all_rows('New Pet Data', 'pets')
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/
2+
# section titled "Simple delete"
3+
#Using the base template, the example code executes a simple delete using named bind variables.
4+
5+
import cx_Oracle
6+
import os
7+
connectString = os.getenv('db_connect')
8+
con = cx_Oracle.connect(connectString)
9+
10+
def get_all_rows(label, data_type='people'):
11+
# Query all rows
12+
cur = con.cursor()
13+
if (data_type == 'pets'):
14+
statement = 'select id, name, owner, type from lcs_pets order by owner, id'
15+
else:
16+
statement = 'select id, name, age, notes from lcs_people order by id'
17+
cur.execute(statement)
18+
res = cur.fetchall()
19+
print(label + ': ')
20+
print (res)
21+
print(' ')
22+
cur.close()
23+
24+
get_all_rows('Original Data', 'pets')
25+
26+
cur = con.cursor()
27+
statement = 'delete from lcs_pets where id = :id'
28+
cur.execute(statement, {'id':1})
29+
con.commit()
30+
31+
get_all_rows('New Data', 'pets')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#Code Sample from the tutorial at https://learncodeshare.net/2015/07/09/delete-crud-using-cx_oracle/
2+
# section titled "Boilerplate template"
3+
#The following code is used as the base template for the other examples.
4+
5+
import cx_Oracle
6+
import os
7+
connectString = os.getenv('db_connect')
8+
con = cx_Oracle.connect(connectString)
9+
10+
def get_all_rows(label, data_type='people'):
11+
# Query all rows
12+
cur = con.cursor()
13+
if (data_type == 'pets'):
14+
statement = 'select id, name, owner, type from lcs_pets order by owner, id'
15+
else:
16+
statement = 'select id, name, age, notes from lcs_people order by id'
17+
cur.execute(statement)
18+
res = cur.fetchall()
19+
print(label + ': ')
20+
print (res)
21+
print(' ')
22+
cur.close()
23+
24+
get_all_rows('Original Data', 'pets')
25+
26+
# Your code here
27+
28+
get_all_rows('New Data', 'pets')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#code Sample from the tutorial at https://learncodeshare.net/2015/06/02/basic-crud-operations-using-cx_oracle/
2+
#The following drops the tables created for the tutorial
3+
4+
drop table CX_PETS
5+
/
6+
7+
drop table CX_PEOPLE
8+
/

0 commit comments

Comments
 (0)