Skip to content

Commit 6038030

Browse files
author
Christopher Jones
authored
Merge pull request oracle-samples#14 from OsBlaineOra/master
Added CRUD examples for Python
2 parents 758853f + 8fe2ac4 commit 6038030

31 files changed

+944
-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](https://learncodeshare.net). The examples are written with Python 3 but they should be easily converted to Python 2.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
# These scripts use identity columns available in Oracle 12.1. If you are using an older version you will need
4+
# to add your own trigger/sequence functionality to generate the id values.
5+
6+
CREATE TABLE lcs_people (
7+
id NUMBER GENERATED BY DEFAULT AS identity,
8+
name VARCHAR2(20),
9+
age NUMBER,
10+
notes VARCHAR2(100)
11+
)
12+
/
13+
14+
ALTER TABLE LCS_PEOPLE
15+
ADD CONSTRAINT PK_LCS_PEOPLE PRIMARY KEY ("ID")
16+
/
17+
18+
CREATE TABLE LCS_PETS (
19+
id NUMBER GENERATED BY DEFAULT AS IDENTITY,
20+
name VARCHAR2(20),
21+
owner NUMBER,
22+
type VARCHAR2(100)
23+
)
24+
/
25+
26+
ALTER TABLE LCS_PETS ADD CONSTRAINT PK_LCS_PETS PRIMARY KEY ("ID")
27+
/
28+
29+
ALTER TABLE LCS_PETS ADD CONSTRAINT FK_LCS_PETS_OWNER FOREIGN KEY ("OWNER") REFERENCES "LCS_PEOPLE" ("ID")
30+
/
31+
32+
INSERT INTO lcs_people (name, age, notes)
33+
VALUES ('Bob', 35, 'I like dogs')
34+
/
35+
36+
INSERT INTO lcs_people (name, age, notes)
37+
VALUES ('Kim', 27, 'I like birds')
38+
/
39+
40+
INSERT INTO lcs_pets (name, owner, type)
41+
VALUES ('Duke', 1, 'dog')
42+
/
43+
44+
INSERT INTO lcs_pets (name, owner, type)
45+
VALUES ('Pepe', 2, 'bird')
46+
/
47+
48+
COMMIT
49+
/
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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 = :new_owner where owner = :old_owner'
31+
cur.execute(statement, {'new_owner':2, 'old_owner':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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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 = [{'id':1, 'name':'Bob', 'age':35, 'notes':'I like dogs'},
33+
{'id':2, 'name':'Kim', 'age':27, 'notes':'I like birds'},
34+
{'id':3, 'name':'Cheryl', 'age':23, 'notes':'I like horses'},
35+
{'id':4, 'name':'Bob', 'age':27, 'notes':'I like rabbits'},
36+
{'id':5, 'name':'Stacey', 'age':45, 'notes':'I like snakes'},
37+
{'id':6, 'name':'Pete', 'age':23, 'notes':'I like cats'},
38+
{'id':7, 'name':'Pat', 'age':36, 'notes':'I like dogs'}]
39+
cur.bindarraysize = 2
40+
cur.executemany("insert into lcs_people(id, name, age, notes) values (:id, :name, :age, :notes)", rows)
41+
con.commit()
42+
43+
# Insert default rows
44+
rows = [{'id':1, 'name':'Duke', 'owner':1, 'type':'dog'},
45+
{'id':2, 'name':'Dragon', 'owner':2, 'type':'bird'},
46+
{'id':3, 'name':'Sneaky', 'owner':5, 'type':'snake'},
47+
{'id':4, 'name':'Red', 'owner':2, 'type':'bird'},
48+
{'id':5, 'name':'Red', 'owner':3, 'type':'horse'},
49+
{'id':6, 'name':'Buster', 'owner':1, 'type':'dog'},
50+
{'id':7, 'name':'Fido', 'owner':7, 'type':'cat'}]
51+
cur.bindarraysize = 2
52+
cur.executemany("insert into lcs_pets (id, name, owner, type) values (:id, :name, :owner, :type)", rows)
53+
con.commit()
54+
55+
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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') # The environment variable for the connect string: DB_CONNECT=user/password@database
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)