From e591f92c3b520d60816063adfe5aa40248e79bf6 Mon Sep 17 00:00:00 2001
From: Blaine Carter Preface
preconfigured with Oracle Database, Python 3.6, cx_Oracle 6.0, a
text editor, and access to a terminal console.
To set up a similar environment yourself, install:
+To set up a similar environment yourself, install the following required software:
-It is easist to have a local pluggable database with the service - 'orclpdb' configured. If your database is not local, or has a - different service, you will need to modify the connect string in all - samples.
+To create the schema run:
+To create the schema run:
sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples+
The database connection information is set in two files: +
The username is "pythonhol" with
+ the password "welcome". The connect string is "localhost/orclpdb".
+ See sql/SampleEnv.sql
.
It is easist to have a local pluggable database with the service + 'orclpdb' configured. If your database is not local, or has a + different service, you will need to modify the connection information in db_config.py and db_config.sql.
+The following sections may need adjusting, depending on how you have set up your environment.
@@ -138,10 +156,6 @@Use the Desktop icons to start editors and terminal windows.
- A pre-created schema exists. The username is "pythonhol" with
- the password "welcome". The connect string is "localhost/orclpdb".
- See sql/SampleEnv.sql
.
If you are new to Python review the Appendix: Python Primer to gain an understanding of the language.
@@ -156,11 +170,32 @@Review the code contained in db_config.py
and db_config.sql
:
db_config.py
+ +user="pythonhol" +pw="welcome" +dsn="localhost/orclpdb" ++
db_config.sql
+ +def user = "pythonhol" +def pw = "welcome" +def connect_string = "localhost/orclpdb" ++
Modify the values in both files to match the connection information for your environment.
+ +Review the code contained in connect.py
:
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print("Database version:", con.version)@@ -192,7 +227,7 @@
There are no statement terminators or begin/end keywords or braces to indicate blocks of code.
@@ -202,7 +237,9 @@import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print("Database version:", con.version)@@ -224,20 +261,24 @@
Open query.py
in an editor. It looks like:
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn)
Edit the file and add the code shown in bold below:
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -261,7 +302,7 @@1.3 Executing a query
Connections and other resources used by cx_Oracle will automatically be closed at the end of scope. This is a common @@ -275,7 +316,9 @@
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -296,7 +339,9 @@1.4 Closing connections
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -323,7 +368,9 @@1.5 Checking versions
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version)@@ -337,7 +384,9 @@1.5 Checking versions
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version) print("Database version:", con.version) @@ -373,8 +422,9 @@1.5 Checking versions
import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -445,8 +495,9 @@2.2 Session pool experiments
import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -533,7 +584,9 @@2.3 Creating a DRCP Connection
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb:pooled", +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn + ":pooled", cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF) print("Database version:", con.version)@@ -575,7 +628,7 @@2.4 Session pooling and DRCP
import cx_Oracle import threading -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb:pooled", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled", min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -658,7 +711,9 @@2.5 More DRCP investigation
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -691,8 +746,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -720,8 +776,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -761,8 +818,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor(scrollable = True) cur.execute("select * from dept order by deptno") @@ -826,8 +884,9 @@2.5 More DRCP investigation
import cx_Oracle import time +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) start = time.time() @@ -898,8 +957,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.prepare("select * from dept where deptno = :id order by deptno") @@ -952,8 +1012,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), @@ -1018,8 +1079,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), @@ -1067,6 +1129,7 @@2.5 More DRCP investigation
Spatial Data Objects (SDO).In a terminal window, start SQL*Plus:
+(Modify the following command to use your connection values.)
sqlplus pythonhol/welcome@localhost/orclpdb @@ -1094,8 +1157,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table @@ -1257,8 +1321,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() res = cur.callfunc('myfunc', int, ('abc', 2)) @@ -1302,8 +1367,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() myvar = cur.var(int) @@ -1350,8 +1416,9 @@6.1 Basic output type handler
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Standard output...") @@ -1407,8 +1474,9 @@6.1 Basic output type handler
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() for value, in cur.execute("select 0.1 from dual"): @@ -1429,8 +1497,9 @@6.1 Basic output type handler
import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale): @@ -1481,8 +1550,9 @@6.1 Basic output type handler
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table @@ -1600,8 +1670,9 @@7.1 Fetching a CLOB using a locator
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") @@ -1648,8 +1719,9 @@7.2 Fetching a CLOB as a string
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") @@ -1704,8 +1776,9 @@7.2 Fetching a CLOB as a string
import collections import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select deptno, dname from dept") @@ -1782,7 +1855,7 @@7.2 Fetching a CLOB as a string
def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) con = MyConnection() cur = con.cursor() @@ -1826,7 +1899,7 @@7.2 Fetching a CLOB as a string
def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) def cursor(self): return MyCursor(self) @@ -1879,8 +1952,9 @@7.2 Fetching a CLOB as a string
import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/aq.py b/samples/tutorial/aq.py index bcfb51fe..a7a9564a 100644 --- a/samples/tutorial/aq.py +++ b/samples/tutorial/aq.py @@ -10,8 +10,9 @@ import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/bind_insert.py b/samples/tutorial/bind_insert.py index bbda40a4..eb10195e 100644 --- a/samples/tutorial/bind_insert.py +++ b/samples/tutorial/bind_insert.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), diff --git a/samples/tutorial/bind_insert.sql b/samples/tutorial/bind_insert.sql index 1b908352..008761d5 100644 --- a/samples/tutorial/bind_insert.sql +++ b/samples/tutorial/bind_insert.sql @@ -8,9 +8,19 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql -drop table mytab; +connect &user/&pw@&connect_string + +begin + execute immediate 'drop table mytab'; +exception +when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +/ create table mytab (id number, data varchar2(20), constraint my_pk primary key (id)); diff --git a/samples/tutorial/bind_query.py b/samples/tutorial/bind_query.py index 2b23c224..8ab8540e 100644 --- a/samples/tutorial/bind_query.py +++ b/samples/tutorial/bind_query.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.prepare("select * from dept where deptno = :id order by deptno") diff --git a/samples/tutorial/bind_sdo.py b/samples/tutorial/bind_sdo.py index 96dde299..3292d23b 100644 --- a/samples/tutorial/bind_sdo.py +++ b/samples/tutorial/bind_sdo.py @@ -9,7 +9,9 @@ from __future__ import print_function import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() diff --git a/samples/tutorial/clob.py b/samples/tutorial/clob.py index 8386e6e6..d7f3d389 100644 --- a/samples/tutorial/clob.py +++ b/samples/tutorial/clob.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") diff --git a/samples/tutorial/clob_string.py b/samples/tutorial/clob_string.py index 3e64db8e..65eeb9b4 100644 --- a/samples/tutorial/clob_string.py +++ b/samples/tutorial/clob_string.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") diff --git a/samples/tutorial/connect.py b/samples/tutorial/connect.py index 36e7a0a4..5686611a 100644 --- a/samples/tutorial/connect.py +++ b/samples/tutorial/connect.py @@ -9,6 +9,7 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print("Database version:", con.version) diff --git a/samples/tutorial/connect_drcp.py b/samples/tutorial/connect_drcp.py index e6e4511b..335748ad 100644 --- a/samples/tutorial/connect_drcp.py +++ b/samples/tutorial/connect_drcp.py @@ -9,7 +9,8 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb:pooled", +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn + ":pooled", cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF) print("Database version:", con.version) diff --git a/samples/tutorial/connect_pool.py b/samples/tutorial/connect_pool.py index 474d8fdd..baed93cf 100644 --- a/samples/tutorial/connect_pool.py +++ b/samples/tutorial/connect_pool.py @@ -10,8 +10,9 @@ import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): diff --git a/samples/tutorial/connect_pool2.py b/samples/tutorial/connect_pool2.py index 12dd5c16..8e0034ae 100644 --- a/samples/tutorial/connect_pool2.py +++ b/samples/tutorial/connect_pool2.py @@ -10,9 +10,10 @@ import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", - min = 2, max = 5, increment = 1, threaded = True) +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, + min = 2, max = 5, increment = 1, threaded = True) def Query(): con = pool.acquire() diff --git a/samples/tutorial/db_config.py b/samples/tutorial/db_config.py new file mode 100644 index 00000000..96fbcde3 --- /dev/null +++ b/samples/tutorial/db_config.py @@ -0,0 +1,3 @@ +user="pythonhol" +pw="welcome" +dsn="localhost/orclpdb" diff --git a/samples/tutorial/db_config.sql b/samples/tutorial/db_config.sql new file mode 100644 index 00000000..0f02b1b4 --- /dev/null +++ b/samples/tutorial/db_config.sql @@ -0,0 +1,3 @@ +def user = "pythonhol" +def pw = "welcome" +def connect_string = "localhost/orclpdb" diff --git a/samples/tutorial/plsql_func.py b/samples/tutorial/plsql_func.py index b8173eae..12dd0656 100644 --- a/samples/tutorial/plsql_func.py +++ b/samples/tutorial/plsql_func.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() res = cur.callfunc('myfunc', int, ('abc', 2)) diff --git a/samples/tutorial/plsql_func.sql b/samples/tutorial/plsql_func.sql index 69f653a5..2a857150 100644 --- a/samples/tutorial/plsql_func.sql +++ b/samples/tutorial/plsql_func.sql @@ -8,7 +8,17 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql + +begin + execute immediate 'drop table ptab'; +exception +when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +/ create table ptab (mydata varchar(20), myid number); diff --git a/samples/tutorial/plsql_proc.py b/samples/tutorial/plsql_proc.py index a378fca9..58c3a366 100644 --- a/samples/tutorial/plsql_proc.py +++ b/samples/tutorial/plsql_proc.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() myvar = cur.var(int) diff --git a/samples/tutorial/plsql_proc.sql b/samples/tutorial/plsql_proc.sql index ff6da9d8..262aa1ad 100644 --- a/samples/tutorial/plsql_proc.sql +++ b/samples/tutorial/plsql_proc.sql @@ -8,7 +8,7 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql create or replace procedure myproc(v1_p in number, v2_p out number) as begin diff --git a/samples/tutorial/query.py b/samples/tutorial/query.py index ce6eb0fa..344c2da7 100644 --- a/samples/tutorial/query.py +++ b/samples/tutorial/query.py @@ -9,5 +9,6 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) diff --git a/samples/tutorial/query2.py b/samples/tutorial/query2.py index c74bfa5e..60695d91 100644 --- a/samples/tutorial/query2.py +++ b/samples/tutorial/query2.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute('select * from dept order by deptno') diff --git a/samples/tutorial/query_arraysize.py b/samples/tutorial/query_arraysize.py index 9a36ba07..c8ff24b6 100644 --- a/samples/tutorial/query_arraysize.py +++ b/samples/tutorial/query_arraysize.py @@ -10,8 +10,9 @@ import cx_Oracle import time +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) start = time.time() diff --git a/samples/tutorial/query_arraysize.sql b/samples/tutorial/query_arraysize.sql index da64a534..3d5ec6ef 100644 --- a/samples/tutorial/query_arraysize.sql +++ b/samples/tutorial/query_arraysize.sql @@ -8,9 +8,17 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql -drop table bigtab; +begin + execute immediate 'drop table bigtab'; +exception +when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +/ create table bigtab (mycol varchar2(20)); begin diff --git a/samples/tutorial/query_many.py b/samples/tutorial/query_many.py index e842e85a..beb0b09b 100644 --- a/samples/tutorial/query_many.py +++ b/samples/tutorial/query_many.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/query_one.py b/samples/tutorial/query_one.py index 094c7734..978fbaff 100644 --- a/samples/tutorial/query_one.py +++ b/samples/tutorial/query_one.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/query_scroll.py b/samples/tutorial/query_scroll.py index d304e550..b0630f94 100644 --- a/samples/tutorial/query_scroll.py +++ b/samples/tutorial/query_scroll.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor(scrollable = True) cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/rowfactory.py b/samples/tutorial/rowfactory.py index d7e0e17c..9dad94dc 100644 --- a/samples/tutorial/rowfactory.py +++ b/samples/tutorial/rowfactory.py @@ -10,8 +10,9 @@ import collections import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select deptno, dname from dept") diff --git a/samples/tutorial/sql/GenerateManySamples.sql b/samples/tutorial/sql/GenerateManySamples.sql new file mode 100644 index 00000000..543d0af9 --- /dev/null +++ b/samples/tutorial/sql/GenerateManySamples.sql @@ -0,0 +1,83 @@ +set serveroutput on; + +declare + v_base_user varchar2(100) := 'pythonhol'; + v_user varchar2(100); + v_pw varchar2(100) := 'welcome'; + v_count number := 50; +begin +--Drop Samples + begin + for r in (select username + from dba_users + where username like ( upper(v_base_user) || '\_%' ) ESCAPE '\') loop + execute immediate 'drop user ' || r.username || ' cascade'; + dbms_output.put_line(r.username || ' dropped'); + end loop; + end; + +-- Setup Samples + + for i in 1..v_count loop + v_user := v_base_user || '_' || i; + + execute immediate 'alter session set nls_date_format = ''YYYY-MM-DD HH24:MI:SS'''; + execute immediate 'alter session set nls_numeric_characters=''.,'''; + execute immediate 'create user ' || v_user || ' identified by ' || v_pw || ' quota unlimited on users default tablespace users'; + execute immediate 'grant + create session, + create table, + create procedure, + create type, + select any dictionary, + change notification to '|| v_user; + + execute immediate 'grant execute on dbms_aqadm to ' || v_user; + execute immediate 'grant execute on dbms_lock to ' || v_user; + execute immediate 'create table ' || v_user || '.testclobs (id number not null, myclob clob not null)'; + +-- Sequence for connect_pool.py + execute immediate 'create sequence ' || v_user || '.myseq'; + +-- EMP/DEPT tables + execute immediate 'CREATE TABLE ' || v_user || '.EMP + (EMPNO NUMBER(4) NOT NULL, + ENAME VARCHAR2(10), + JOB VARCHAR2(9), + MGR NUMBER(4), + HIREDATE DATE, + SAL NUMBER(7, 2), + COMM NUMBER(7, 2), + DEPTNO NUMBER(2))'; + execute immediate 'CREATE TABLE ' || v_user || '.DEPT + (DEPTNO NUMBER(2), + DNAME VARCHAR2(14), + LOC VARCHAR2(13) )'; + + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (10, ''ACCOUNTING'', ''NEW YORK'')'; + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (20, ''RESEARCH'', ''DALLAS'')'; + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (30, ''SALES'', ''CHICAGO'')'; + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (40, ''OPERATIONS'', ''BOSTON'')'; + + execute immediate 'CREATE TABLE ' || v_user || '.BONUS + (ENAME VARCHAR2(10), + JOB VARCHAR2(9), + SAL NUMBER, + COMM NUMBER)'; + + execute immediate 'CREATE TABLE ' || v_user || '.SALGRADE + (GRADE NUMBER, + LOSAL NUMBER, + HISAL NUMBER)'; + + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (1, 700, 1200)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (2, 1201, 1400)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (3, 1401, 2000)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (4, 2001, 3000)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (5, 3001, 9999)'; + + commit; + dbms_output.put_line(v_user || ' Created'); + end loop; + +end; diff --git a/samples/tutorial/sql/SampleEnv.sql b/samples/tutorial/sql/SampleEnv.sql index 903952a0..9c8d7c98 100644 --- a/samples/tutorial/sql/SampleEnv.sql +++ b/samples/tutorial/sql/SampleEnv.sql @@ -13,7 +13,7 @@ set echo off termout on feedback off verify off -define main_user = "pythonhol" -- $CX_ORACLE_SAMPLES_MAIN_USER +define main_user = "pythonhol2" -- $CX_ORACLE_SAMPLES_MAIN_USER define main_password = "welcome" -- $CX_ORACLE_SAMPLES_MAIN_PASSWORD prompt ************************************************************************ diff --git a/samples/tutorial/subclass.py b/samples/tutorial/subclass.py index e429c7f0..22cd976c 100644 --- a/samples/tutorial/subclass.py +++ b/samples/tutorial/subclass.py @@ -9,12 +9,13 @@ from __future__ import print_function import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) con = MyConnection() cur = con.cursor() diff --git a/samples/tutorial/type_converter.py b/samples/tutorial/type_converter.py index b928666f..db0d3b63 100644 --- a/samples/tutorial/type_converter.py +++ b/samples/tutorial/type_converter.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() for value, in cur.execute("select 0.1 from dual"): diff --git a/samples/tutorial/type_input.py b/samples/tutorial/type_input.py index e22ed817..a3731e14 100644 --- a/samples/tutorial/type_input.py +++ b/samples/tutorial/type_input.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table diff --git a/samples/tutorial/type_output.py b/samples/tutorial/type_output.py index b7b5cc6a..9e4e2e66 100644 --- a/samples/tutorial/type_output.py +++ b/samples/tutorial/type_output.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Standard output...") diff --git a/samples/tutorial/versions.py b/samples/tutorial/versions.py index 599d8de7..f8344c2c 100644 --- a/samples/tutorial/versions.py +++ b/samples/tutorial/versions.py @@ -9,7 +9,8 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect('pythonhol/welcome@localhost/orclpdb') +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version) diff --git a/setup.py b/setup.py index cce675f9..3f57d61f 100644 --- a/setup.py +++ b/setup.py @@ -70,22 +70,41 @@ def run(self): sourceDir = "src" sources = [os.path.join(sourceDir, n) \ for n in sorted(os.listdir(sourceDir)) if n.endswith(".c")] - -# define ODPI-C sources -dpiSourceDir = os.path.join("odpi", "src") -dpiSources = [os.path.join(dpiSourceDir, n) \ - for n in sorted(os.listdir(dpiSourceDir)) if n.endswith(".c")] +depends = ["src/cxoModule.h"] + + +# define ODPI-C sources, libraries and include directories; if the environment +# variables ODPIC_INC_DIR and ODPIC_LIB_DIR are both set, assume these +# locations contain a compiled installation of ODPI-C; otherwise, use the +# source of ODPI-C found in the odpi subdirectory +dpiIncludeDir = os.environ.get("ODPIC_INC_DIR") +dpiLibDir = os.environ.get("ODPIC_LIB_DIR") +if dpiIncludeDir and dpiLibDir: + dpiSources = [] + includeDirs = [dpiIncludeDir] + libraries = ["odpic"] + libraryDirs = [dpiLibDir] +else: + includeDirs = ["odpi/include", "odpi/src"] + dpiSourceDir = os.path.join("odpi", "src") + dpiSources = [os.path.join(dpiSourceDir, n) \ + for n in sorted(os.listdir(dpiSourceDir)) if n.endswith(".c")] + depends.extend(["odpi/include/dpi.h", "odpi/src/dpiImpl.h", + "odpi/src/dpiErrorMessages.h"]) + libraries = [] + libraryDirs = [] # setup the extension extension = Extension( name = "cx_Oracle", - include_dirs = ["odpi/include", "odpi/src"], + include_dirs = includeDirs, extra_compile_args = extraCompileArgs, define_macros = [("CXO_BUILD_VERSION", BUILD_VERSION)], extra_link_args = extraLinkArgs, sources = sources + dpiSources, - depends = ["src/cxoModule.h", "odpi/include/dpi.h", - "odpi/src/dpiImpl.h", "odpi/src/dpiErrorMessages.h"]) + depends = depends, + libraries = libraries, + library_dirs = libraryDirs) # perform the setup setup( From 0043381118ef37be68b655b66534719ea81b0ab5 Mon Sep 17 00:00:00 2001 From: Blaine Carter-Date: Fri, 20 Apr 2018 13:32:12 -0600 Subject: [PATCH 02/18] fixed to create a pool instead of a regular connection. --- samples/tutorial/connect_pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/tutorial/connect_pool.py b/samples/tutorial/connect_pool.py index baed93cf..b4c5bc3d 100644 --- a/samples/tutorial/connect_pool.py +++ b/samples/tutorial/connect_pool.py @@ -12,7 +12,7 @@ import threading import db_config -con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn, +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): From aa75fc9a5d7f3f7d99ec85d9d6d3891af83e2309 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 14:08:31 -0600 Subject: [PATCH 03/18] added connect line --- samples/tutorial/bind_insert.sql | 1 - samples/tutorial/plsql_func.sql | 1 + samples/tutorial/plsql_proc.sql | 1 + samples/tutorial/query_arraysize.sql | 1 + 4 files changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/tutorial/bind_insert.sql b/samples/tutorial/bind_insert.sql index 008761d5..068e0880 100644 --- a/samples/tutorial/bind_insert.sql +++ b/samples/tutorial/bind_insert.sql @@ -9,7 +9,6 @@ set echo on @@db_config.sql - connect &user/&pw@&connect_string begin diff --git a/samples/tutorial/plsql_func.sql b/samples/tutorial/plsql_func.sql index 2a857150..1c5c24f1 100644 --- a/samples/tutorial/plsql_func.sql +++ b/samples/tutorial/plsql_func.sql @@ -9,6 +9,7 @@ set echo on @@db_config.sql +connect &user/&pw@&connect_string begin execute immediate 'drop table ptab'; diff --git a/samples/tutorial/plsql_proc.sql b/samples/tutorial/plsql_proc.sql index 262aa1ad..34b19cf6 100644 --- a/samples/tutorial/plsql_proc.sql +++ b/samples/tutorial/plsql_proc.sql @@ -9,6 +9,7 @@ set echo on @@db_config.sql +connect &user/&pw@&connect_string create or replace procedure myproc(v1_p in number, v2_p out number) as begin diff --git a/samples/tutorial/query_arraysize.sql b/samples/tutorial/query_arraysize.sql index 3d5ec6ef..b4702145 100644 --- a/samples/tutorial/query_arraysize.sql +++ b/samples/tutorial/query_arraysize.sql @@ -9,6 +9,7 @@ set echo on @@db_config.sql +connect &user/&pw@&connect_string begin execute immediate 'drop table bigtab'; From 91ab21b27836eb8e05a2a1bf9a97f845a932f2a8 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 14:42:40 -0600 Subject: [PATCH 04/18] removed extra crlf --- samples/tutorial/bind_sdo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/tutorial/bind_sdo.py b/samples/tutorial/bind_sdo.py index 3292d23b..164ca7c6 100644 --- a/samples/tutorial/bind_sdo.py +++ b/samples/tutorial/bind_sdo.py @@ -12,7 +12,6 @@ import db_config con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) - cur = con.cursor() # Create table From 6d86adc5e8e711fd391e538fff0a16cc4048d7a4 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 14:43:15 -0600 Subject: [PATCH 05/18] Cleaned up text, fixed some errors, fixed table of contents --- ...Database-12c-Scripting-for-the-Future.html | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html index 4b1d2619..88dda247 100644 --- a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html +++ b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html @@ -17,16 +17,18 @@ Contents
- Preface
+- Connection Information
- Overview
- Using Python cx_Oracle 6 with Oracle Database 12c
- 1. Connecting to Oracle
-
- 1.1 Creating a basic connection
-- 1.2 Indentation indicates code structure
-- 1.3 Executing a query
-- 1.4 Closing connections
-- 1.5 Checking versions
+- 1.1 Set the connection values
+- 1.2 Creating a basic connection
+- 1.3 Indentation indicates code structure
+- 1.4 Executing a query
+- 1.5 Closing connections
+- 1.6 Checking versions
- 2. Connection Pooling @@ -123,7 +125,7 @@
Preface
sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamplesConnection Information
+Connection Information
The database connection information is set in two files:
@@ -333,7 +335,9 @@
1.5 Closing connections
python query.py-This gives the error "cx_Oracle.DatabaseError: DPI-1054: connection cannot be closed when open statements or LOBs exist".
+If you are using cx_Oracle 6.2+ the connection will close and you can skip ahead to section 1.6.
+ +If you are using an older version of cx_Oracle, this gives the error "cx_Oracle.DatabaseError: DPI-1054: connection cannot be closed when open statements or LOBs exist".
To fix this, edit the file and close the cursor:
@@ -362,7 +366,7 @@1.5 Closing connections
Review the code contained in versions.py
:
import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): @@ -1894,6 +1899,7 @@7.2 Fetching a CLOB as a string
import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): From 1c6555d89d51307fb6b88dc9a22ea9acf63b9f8d Mon Sep 17 00:00:00 2001 From: Blaine CarterDate: Fri, 20 Apr 2018 15:36:04 -0600 Subject: [PATCH 06/18] removed generatemany file. Fixed indent in connect_pool2. Fixed user in sampleenv. --- samples/tutorial/connect_pool2.py | 2 +- samples/tutorial/db_config.py | 6 +- samples/tutorial/sql/GenerateManySamples.sql | 83 -------------------- samples/tutorial/sql/SampleEnv.sql | 2 +- 4 files changed, 5 insertions(+), 88 deletions(-) delete mode 100644 samples/tutorial/sql/GenerateManySamples.sql diff --git a/samples/tutorial/connect_pool2.py b/samples/tutorial/connect_pool2.py index 8e0034ae..8abddbf8 100644 --- a/samples/tutorial/connect_pool2.py +++ b/samples/tutorial/connect_pool2.py @@ -13,7 +13,7 @@ import db_config pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, - min = 2, max = 5, increment = 1, threaded = True) + min = 2, max = 5, increment = 1, threaded = True) def Query(): con = pool.acquire() diff --git a/samples/tutorial/db_config.py b/samples/tutorial/db_config.py index 96fbcde3..8e785678 100644 --- a/samples/tutorial/db_config.py +++ b/samples/tutorial/db_config.py @@ -1,3 +1,3 @@ -user="pythonhol" -pw="welcome" -dsn="localhost/orclpdb" +user = "pythonhol" +pw = "welcome" +dsn = "localhost/orclpdb" diff --git a/samples/tutorial/sql/GenerateManySamples.sql b/samples/tutorial/sql/GenerateManySamples.sql deleted file mode 100644 index 543d0af9..00000000 --- a/samples/tutorial/sql/GenerateManySamples.sql +++ /dev/null @@ -1,83 +0,0 @@ -set serveroutput on; - -declare - v_base_user varchar2(100) := 'pythonhol'; - v_user varchar2(100); - v_pw varchar2(100) := 'welcome'; - v_count number := 50; -begin ---Drop Samples - begin - for r in (select username - from dba_users - where username like ( upper(v_base_user) || '\_%' ) ESCAPE '\') loop - execute immediate 'drop user ' || r.username || ' cascade'; - dbms_output.put_line(r.username || ' dropped'); - end loop; - end; - --- Setup Samples - - for i in 1..v_count loop - v_user := v_base_user || '_' || i; - - execute immediate 'alter session set nls_date_format = ''YYYY-MM-DD HH24:MI:SS'''; - execute immediate 'alter session set nls_numeric_characters=''.,'''; - execute immediate 'create user ' || v_user || ' identified by ' || v_pw || ' quota unlimited on users default tablespace users'; - execute immediate 'grant - create session, - create table, - create procedure, - create type, - select any dictionary, - change notification to '|| v_user; - - execute immediate 'grant execute on dbms_aqadm to ' || v_user; - execute immediate 'grant execute on dbms_lock to ' || v_user; - execute immediate 'create table ' || v_user || '.testclobs (id number not null, myclob clob not null)'; - --- Sequence for connect_pool.py - execute immediate 'create sequence ' || v_user || '.myseq'; - --- EMP/DEPT tables - execute immediate 'CREATE TABLE ' || v_user || '.EMP - (EMPNO NUMBER(4) NOT NULL, - ENAME VARCHAR2(10), - JOB VARCHAR2(9), - MGR NUMBER(4), - HIREDATE DATE, - SAL NUMBER(7, 2), - COMM NUMBER(7, 2), - DEPTNO NUMBER(2))'; - execute immediate 'CREATE TABLE ' || v_user || '.DEPT - (DEPTNO NUMBER(2), - DNAME VARCHAR2(14), - LOC VARCHAR2(13) )'; - - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (10, ''ACCOUNTING'', ''NEW YORK'')'; - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (20, ''RESEARCH'', ''DALLAS'')'; - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (30, ''SALES'', ''CHICAGO'')'; - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (40, ''OPERATIONS'', ''BOSTON'')'; - - execute immediate 'CREATE TABLE ' || v_user || '.BONUS - (ENAME VARCHAR2(10), - JOB VARCHAR2(9), - SAL NUMBER, - COMM NUMBER)'; - - execute immediate 'CREATE TABLE ' || v_user || '.SALGRADE - (GRADE NUMBER, - LOSAL NUMBER, - HISAL NUMBER)'; - - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (1, 700, 1200)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (2, 1201, 1400)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (3, 1401, 2000)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (4, 2001, 3000)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (5, 3001, 9999)'; - - commit; - dbms_output.put_line(v_user || ' Created'); - end loop; - -end; diff --git a/samples/tutorial/sql/SampleEnv.sql b/samples/tutorial/sql/SampleEnv.sql index 9c8d7c98..903952a0 100644 --- a/samples/tutorial/sql/SampleEnv.sql +++ b/samples/tutorial/sql/SampleEnv.sql @@ -13,7 +13,7 @@ set echo off termout on feedback off verify off -define main_user = "pythonhol2" -- $CX_ORACLE_SAMPLES_MAIN_USER +define main_user = "pythonhol" -- $CX_ORACLE_SAMPLES_MAIN_USER define main_password = "welcome" -- $CX_ORACLE_SAMPLES_MAIN_PASSWORD prompt ************************************************************************ From 5ab03e7d8ad2d9ec177da6c8663b1e7a3a375672 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 15:40:42 -0600 Subject: [PATCH 07/18] attempting to reset the file --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 3f57d61f..08ade4eb 100644 --- a/setup.py +++ b/setup.py @@ -124,4 +124,3 @@ def run(self): keywords = "Oracle", license = "BSD License", classifiers = classifiers) - From 9a019de3b68bc638dfafb1de2476f291d2f62d18 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 12:15:22 -0600 Subject: [PATCH 08/18] Modified samples and instructions to import common connection information from db_config.py or db_config.sql Changed python modules to use enviroment variables to connect. Changed .sql scripts to hand errors when droping tables and removed connect lines. --- ...Database-12c-Scripting-for-the-Future.html | 180 ++++++++++++------ samples/tutorial/aq.py | 3 +- samples/tutorial/bind_insert.py | 3 +- samples/tutorial/bind_insert.sql | 14 +- samples/tutorial/bind_query.py | 3 +- samples/tutorial/bind_sdo.py | 4 +- samples/tutorial/clob.py | 3 +- samples/tutorial/clob_string.py | 3 +- samples/tutorial/connect.py | 3 +- samples/tutorial/connect_drcp.py | 3 +- samples/tutorial/connect_pool.py | 3 +- samples/tutorial/connect_pool2.py | 5 +- samples/tutorial/db_config.py | 3 + samples/tutorial/db_config.sql | 3 + samples/tutorial/plsql_func.py | 3 +- samples/tutorial/plsql_func.sql | 12 +- samples/tutorial/plsql_proc.py | 3 +- samples/tutorial/plsql_proc.sql | 2 +- samples/tutorial/query.py | 3 +- samples/tutorial/query2.py | 3 +- samples/tutorial/query_arraysize.py | 3 +- samples/tutorial/query_arraysize.sql | 12 +- samples/tutorial/query_many.py | 3 +- samples/tutorial/query_one.py | 3 +- samples/tutorial/query_scroll.py | 3 +- samples/tutorial/rowfactory.py | 3 +- samples/tutorial/sql/GenerateManySamples.sql | 83 ++++++++ samples/tutorial/sql/SampleEnv.sql | 2 +- samples/tutorial/subclass.py | 3 +- samples/tutorial/type_converter.py | 3 +- samples/tutorial/type_input.py | 3 +- samples/tutorial/type_output.py | 3 +- samples/tutorial/versions.py | 3 +- 33 files changed, 301 insertions(+), 85 deletions(-) create mode 100644 samples/tutorial/db_config.py create mode 100644 samples/tutorial/db_config.sql create mode 100644 samples/tutorial/sql/GenerateManySamples.sql diff --git a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html index e5e0d425..4b1d2619 100644 --- a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html +++ b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html @@ -103,25 +103,43 @@ Preface
preconfigured with Oracle Database, Python 3.6, cx_Oracle 6.0, a text editor, and access to a terminal console. -To set up a similar environment yourself, install:
+To set up a similar environment yourself, install the following required software:
--
- -- Python 3.6
-- cx_Oracle 6.0
-- Oracle Database 12c
-It is easist to have a local pluggable database with the service - 'orclpdb' configured. If your database is not local, or has a - different service, you will need to modify the connect string in all - samples.
++
-- Python (3.6 preferred but 2.7 should work)
+- cx_Oracle (6.2.1 preferred any 6.x should work) and Oracle Instant Client Package - Basic (12.2 preferred 12.1 should work) + +
+- Oracle Instant Client Package - SQL*Plus OR Oracle SQLcl
+To create the schema run:
+To create the schema run:
sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples+Connection Information
+ +The database connection information is set in two files: +
+
+ + +- db_config.py which is imported by the other Python modules.
+- db_config.sql which is used by the other SQL scripts.
+The username is "pythonhol" with + the password "welcome". The connect string is "localhost/orclpdb". + See
+ +sql/SampleEnv.sql
.It is easist to have a local pluggable database with the service + 'orclpdb' configured. If your database is not local, or has a + different service, you will need to modify the connection information in db_config.py and db_config.sql.
+The following sections may need adjusting, depending on how you have set up your environment.
@@ -138,10 +156,6 @@Overview
Use the Desktop icons to start editors and terminal windows.
-A pre-created schema exists. The username is "pythonhol" with - the password "welcome". The connect string is "localhost/orclpdb". - See
-sql/SampleEnv.sql
.If you are new to Python review the Appendix: Python Primer to gain an understanding of the language.
@@ -156,11 +170,32 @@Using Python cx_Oracle 6 with Oracle Database 12c
1. Connecting to Oracle
- -
+ +1.1 Creating a basic connection
+1.1 Set the connection values
+Review the code contained in
+db_config.py
anddb_config.sql
:db_config.py
++user="pythonhol" +pw="welcome" +dsn="localhost/orclpdb" ++db_config.sql
++def user = "pythonhol" +def pw = "welcome" +def connect_string = "localhost/orclpdb" ++Modify the values in both files to match the connection information for your environment.
+ +- +
1.2 Creating a basic connection
Review the code contained in
connect.py
:import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print("Database version:", con.version)@@ -192,7 +227,7 @@1.1 Creating a basic connection
- -
1.2 Indentation indicates code structure
+1.3 Indentation indicates code structure
There are no statement terminators or begin/end keywords or braces to indicate blocks of code.
@@ -202,7 +237,9 @@1.2 Indentation indicates code structure
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print("Database version:", con.version)@@ -224,20 +261,24 @@1.2 Indentation indicates code structure
- -
1.3 Executing a query
+1.4 Executing a query
Open
query.py
in an editor. It looks like:import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn)Edit the file and add the code shown in bold below:
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -261,7 +302,7 @@1.3 Executing a query
- -
1.4 Closing connections
+1.5 Closing connections
Connections and other resources used by cx_Oracle will automatically be closed at the end of scope. This is a common @@ -275,7 +316,9 @@
1.4 Closing connections
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -296,7 +339,9 @@1.4 Closing connections
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -323,7 +368,9 @@1.5 Checking versions
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version)@@ -337,7 +384,9 @@1.5 Checking versions
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version) print("Database version:", con.version) @@ -373,8 +422,9 @@1.5 Checking versions
import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -445,8 +495,9 @@2.2 Session pool experiments
import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -533,7 +584,9 @@2.3 Creating a DRCP Connection
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb:pooled", +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn + ":pooled", cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF) print("Database version:", con.version)@@ -575,7 +628,7 @@2.4 Session pooling and DRCP
import cx_Oracle import threading -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb:pooled", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled", min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -658,7 +711,9 @@2.5 More DRCP investigation
import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -691,8 +746,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -720,8 +776,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") @@ -761,8 +818,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor(scrollable = True) cur.execute("select * from dept order by deptno") @@ -826,8 +884,9 @@2.5 More DRCP investigation
import cx_Oracle import time +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) start = time.time() @@ -898,8 +957,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.prepare("select * from dept where deptno = :id order by deptno") @@ -952,8 +1012,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), @@ -1018,8 +1079,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), @@ -1067,6 +1129,7 @@2.5 More DRCP investigation
Spatial Data Objects (SDO).In a terminal window, start SQL*Plus:
+(Modify the following command to use your connection values.)
sqlplus pythonhol/welcome@localhost/orclpdb @@ -1094,8 +1157,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table @@ -1257,8 +1321,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() res = cur.callfunc('myfunc', int, ('abc', 2)) @@ -1302,8 +1367,9 @@2.5 More DRCP investigation
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() myvar = cur.var(int) @@ -1350,8 +1416,9 @@6.1 Basic output type handler
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Standard output...") @@ -1407,8 +1474,9 @@6.1 Basic output type handler
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() for value, in cur.execute("select 0.1 from dual"): @@ -1429,8 +1497,9 @@6.1 Basic output type handler
import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale): @@ -1481,8 +1550,9 @@6.1 Basic output type handler
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table @@ -1600,8 +1670,9 @@7.1 Fetching a CLOB using a locator
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") @@ -1648,8 +1719,9 @@7.2 Fetching a CLOB as a string
import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") @@ -1704,8 +1776,9 @@7.2 Fetching a CLOB as a string
import collections import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select deptno, dname from dept") @@ -1782,7 +1855,7 @@7.2 Fetching a CLOB as a string
def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) con = MyConnection() cur = con.cursor() @@ -1826,7 +1899,7 @@7.2 Fetching a CLOB as a string
def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) def cursor(self): return MyCursor(self) @@ -1879,8 +1952,9 @@7.2 Fetching a CLOB as a string
import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/aq.py b/samples/tutorial/aq.py index bcfb51fe..a7a9564a 100644 --- a/samples/tutorial/aq.py +++ b/samples/tutorial/aq.py @@ -10,8 +10,9 @@ import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/bind_insert.py b/samples/tutorial/bind_insert.py index bbda40a4..eb10195e 100644 --- a/samples/tutorial/bind_insert.py +++ b/samples/tutorial/bind_insert.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), diff --git a/samples/tutorial/bind_insert.sql b/samples/tutorial/bind_insert.sql index 1b908352..008761d5 100644 --- a/samples/tutorial/bind_insert.sql +++ b/samples/tutorial/bind_insert.sql @@ -8,9 +8,19 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql -drop table mytab; +connect &user/&pw@&connect_string + +begin + execute immediate 'drop table mytab'; +exception +when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +/ create table mytab (id number, data varchar2(20), constraint my_pk primary key (id)); diff --git a/samples/tutorial/bind_query.py b/samples/tutorial/bind_query.py index 2b23c224..8ab8540e 100644 --- a/samples/tutorial/bind_query.py +++ b/samples/tutorial/bind_query.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.prepare("select * from dept where deptno = :id order by deptno") diff --git a/samples/tutorial/bind_sdo.py b/samples/tutorial/bind_sdo.py index 96dde299..3292d23b 100644 --- a/samples/tutorial/bind_sdo.py +++ b/samples/tutorial/bind_sdo.py @@ -9,7 +9,9 @@ from __future__ import print_function import cx_Oracle -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() diff --git a/samples/tutorial/clob.py b/samples/tutorial/clob.py index 8386e6e6..d7f3d389 100644 --- a/samples/tutorial/clob.py +++ b/samples/tutorial/clob.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") diff --git a/samples/tutorial/clob_string.py b/samples/tutorial/clob_string.py index 3e64db8e..65eeb9b4 100644 --- a/samples/tutorial/clob_string.py +++ b/samples/tutorial/clob_string.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Inserting data...") diff --git a/samples/tutorial/connect.py b/samples/tutorial/connect.py index 36e7a0a4..5686611a 100644 --- a/samples/tutorial/connect.py +++ b/samples/tutorial/connect.py @@ -9,6 +9,7 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print("Database version:", con.version) diff --git a/samples/tutorial/connect_drcp.py b/samples/tutorial/connect_drcp.py index e6e4511b..335748ad 100644 --- a/samples/tutorial/connect_drcp.py +++ b/samples/tutorial/connect_drcp.py @@ -9,7 +9,8 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb:pooled", +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn + ":pooled", cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF) print("Database version:", con.version) diff --git a/samples/tutorial/connect_pool.py b/samples/tutorial/connect_pool.py index 474d8fdd..baed93cf 100644 --- a/samples/tutorial/connect_pool.py +++ b/samples/tutorial/connect_pool.py @@ -10,8 +10,9 @@ import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): diff --git a/samples/tutorial/connect_pool2.py b/samples/tutorial/connect_pool2.py index 12dd5c16..8e0034ae 100644 --- a/samples/tutorial/connect_pool2.py +++ b/samples/tutorial/connect_pool2.py @@ -10,9 +10,10 @@ import cx_Oracle import threading +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb", - min = 2, max = 5, increment = 1, threaded = True) +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, + min = 2, max = 5, increment = 1, threaded = True) def Query(): con = pool.acquire() diff --git a/samples/tutorial/db_config.py b/samples/tutorial/db_config.py new file mode 100644 index 00000000..96fbcde3 --- /dev/null +++ b/samples/tutorial/db_config.py @@ -0,0 +1,3 @@ +user="pythonhol" +pw="welcome" +dsn="localhost/orclpdb" diff --git a/samples/tutorial/db_config.sql b/samples/tutorial/db_config.sql new file mode 100644 index 00000000..0f02b1b4 --- /dev/null +++ b/samples/tutorial/db_config.sql @@ -0,0 +1,3 @@ +def user = "pythonhol" +def pw = "welcome" +def connect_string = "localhost/orclpdb" diff --git a/samples/tutorial/plsql_func.py b/samples/tutorial/plsql_func.py index b8173eae..12dd0656 100644 --- a/samples/tutorial/plsql_func.py +++ b/samples/tutorial/plsql_func.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() res = cur.callfunc('myfunc', int, ('abc', 2)) diff --git a/samples/tutorial/plsql_func.sql b/samples/tutorial/plsql_func.sql index 69f653a5..2a857150 100644 --- a/samples/tutorial/plsql_func.sql +++ b/samples/tutorial/plsql_func.sql @@ -8,7 +8,17 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql + +begin + execute immediate 'drop table ptab'; +exception +when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +/ create table ptab (mydata varchar(20), myid number); diff --git a/samples/tutorial/plsql_proc.py b/samples/tutorial/plsql_proc.py index a378fca9..58c3a366 100644 --- a/samples/tutorial/plsql_proc.py +++ b/samples/tutorial/plsql_proc.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() myvar = cur.var(int) diff --git a/samples/tutorial/plsql_proc.sql b/samples/tutorial/plsql_proc.sql index ff6da9d8..262aa1ad 100644 --- a/samples/tutorial/plsql_proc.sql +++ b/samples/tutorial/plsql_proc.sql @@ -8,7 +8,7 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql create or replace procedure myproc(v1_p in number, v2_p out number) as begin diff --git a/samples/tutorial/query.py b/samples/tutorial/query.py index ce6eb0fa..344c2da7 100644 --- a/samples/tutorial/query.py +++ b/samples/tutorial/query.py @@ -9,5 +9,6 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) diff --git a/samples/tutorial/query2.py b/samples/tutorial/query2.py index c74bfa5e..60695d91 100644 --- a/samples/tutorial/query2.py +++ b/samples/tutorial/query2.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute('select * from dept order by deptno') diff --git a/samples/tutorial/query_arraysize.py b/samples/tutorial/query_arraysize.py index 9a36ba07..c8ff24b6 100644 --- a/samples/tutorial/query_arraysize.py +++ b/samples/tutorial/query_arraysize.py @@ -10,8 +10,9 @@ import cx_Oracle import time +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) start = time.time() diff --git a/samples/tutorial/query_arraysize.sql b/samples/tutorial/query_arraysize.sql index da64a534..3d5ec6ef 100644 --- a/samples/tutorial/query_arraysize.sql +++ b/samples/tutorial/query_arraysize.sql @@ -8,9 +8,17 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql -drop table bigtab; +begin + execute immediate 'drop table bigtab'; +exception +when others then + if sqlcode not in (-00942) then + raise; + end if; +end; +/ create table bigtab (mycol varchar2(20)); begin diff --git a/samples/tutorial/query_many.py b/samples/tutorial/query_many.py index e842e85a..beb0b09b 100644 --- a/samples/tutorial/query_many.py +++ b/samples/tutorial/query_many.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/query_one.py b/samples/tutorial/query_one.py index 094c7734..978fbaff 100644 --- a/samples/tutorial/query_one.py +++ b/samples/tutorial/query_one.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/query_scroll.py b/samples/tutorial/query_scroll.py index d304e550..b0630f94 100644 --- a/samples/tutorial/query_scroll.py +++ b/samples/tutorial/query_scroll.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor(scrollable = True) cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/rowfactory.py b/samples/tutorial/rowfactory.py index d7e0e17c..9dad94dc 100644 --- a/samples/tutorial/rowfactory.py +++ b/samples/tutorial/rowfactory.py @@ -10,8 +10,9 @@ import collections import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select deptno, dname from dept") diff --git a/samples/tutorial/sql/GenerateManySamples.sql b/samples/tutorial/sql/GenerateManySamples.sql new file mode 100644 index 00000000..543d0af9 --- /dev/null +++ b/samples/tutorial/sql/GenerateManySamples.sql @@ -0,0 +1,83 @@ +set serveroutput on; + +declare + v_base_user varchar2(100) := 'pythonhol'; + v_user varchar2(100); + v_pw varchar2(100) := 'welcome'; + v_count number := 50; +begin +--Drop Samples + begin + for r in (select username + from dba_users + where username like ( upper(v_base_user) || '\_%' ) ESCAPE '\') loop + execute immediate 'drop user ' || r.username || ' cascade'; + dbms_output.put_line(r.username || ' dropped'); + end loop; + end; + +-- Setup Samples + + for i in 1..v_count loop + v_user := v_base_user || '_' || i; + + execute immediate 'alter session set nls_date_format = ''YYYY-MM-DD HH24:MI:SS'''; + execute immediate 'alter session set nls_numeric_characters=''.,'''; + execute immediate 'create user ' || v_user || ' identified by ' || v_pw || ' quota unlimited on users default tablespace users'; + execute immediate 'grant + create session, + create table, + create procedure, + create type, + select any dictionary, + change notification to '|| v_user; + + execute immediate 'grant execute on dbms_aqadm to ' || v_user; + execute immediate 'grant execute on dbms_lock to ' || v_user; + execute immediate 'create table ' || v_user || '.testclobs (id number not null, myclob clob not null)'; + +-- Sequence for connect_pool.py + execute immediate 'create sequence ' || v_user || '.myseq'; + +-- EMP/DEPT tables + execute immediate 'CREATE TABLE ' || v_user || '.EMP + (EMPNO NUMBER(4) NOT NULL, + ENAME VARCHAR2(10), + JOB VARCHAR2(9), + MGR NUMBER(4), + HIREDATE DATE, + SAL NUMBER(7, 2), + COMM NUMBER(7, 2), + DEPTNO NUMBER(2))'; + execute immediate 'CREATE TABLE ' || v_user || '.DEPT + (DEPTNO NUMBER(2), + DNAME VARCHAR2(14), + LOC VARCHAR2(13) )'; + + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (10, ''ACCOUNTING'', ''NEW YORK'')'; + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (20, ''RESEARCH'', ''DALLAS'')'; + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (30, ''SALES'', ''CHICAGO'')'; + execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (40, ''OPERATIONS'', ''BOSTON'')'; + + execute immediate 'CREATE TABLE ' || v_user || '.BONUS + (ENAME VARCHAR2(10), + JOB VARCHAR2(9), + SAL NUMBER, + COMM NUMBER)'; + + execute immediate 'CREATE TABLE ' || v_user || '.SALGRADE + (GRADE NUMBER, + LOSAL NUMBER, + HISAL NUMBER)'; + + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (1, 700, 1200)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (2, 1201, 1400)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (3, 1401, 2000)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (4, 2001, 3000)'; + execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (5, 3001, 9999)'; + + commit; + dbms_output.put_line(v_user || ' Created'); + end loop; + +end; diff --git a/samples/tutorial/sql/SampleEnv.sql b/samples/tutorial/sql/SampleEnv.sql index 903952a0..9c8d7c98 100644 --- a/samples/tutorial/sql/SampleEnv.sql +++ b/samples/tutorial/sql/SampleEnv.sql @@ -13,7 +13,7 @@ set echo off termout on feedback off verify off -define main_user = "pythonhol" -- $CX_ORACLE_SAMPLES_MAIN_USER +define main_user = "pythonhol2" -- $CX_ORACLE_SAMPLES_MAIN_USER define main_password = "welcome" -- $CX_ORACLE_SAMPLES_MAIN_PASSWORD prompt ************************************************************************ diff --git a/samples/tutorial/subclass.py b/samples/tutorial/subclass.py index e429c7f0..22cd976c 100644 --- a/samples/tutorial/subclass.py +++ b/samples/tutorial/subclass.py @@ -9,12 +9,13 @@ from __future__ import print_function import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) con = MyConnection() cur = con.cursor() diff --git a/samples/tutorial/type_converter.py b/samples/tutorial/type_converter.py index b928666f..db0d3b63 100644 --- a/samples/tutorial/type_converter.py +++ b/samples/tutorial/type_converter.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() for value, in cur.execute("select 0.1 from dual"): diff --git a/samples/tutorial/type_input.py b/samples/tutorial/type_input.py index e22ed817..a3731e14 100644 --- a/samples/tutorial/type_input.py +++ b/samples/tutorial/type_input.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table diff --git a/samples/tutorial/type_output.py b/samples/tutorial/type_output.py index b7b5cc6a..9e4e2e66 100644 --- a/samples/tutorial/type_output.py +++ b/samples/tutorial/type_output.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() print("Standard output...") diff --git a/samples/tutorial/versions.py b/samples/tutorial/versions.py index 599d8de7..f8344c2c 100644 --- a/samples/tutorial/versions.py +++ b/samples/tutorial/versions.py @@ -9,7 +9,8 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect('pythonhol/welcome@localhost/orclpdb') +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version) From 3264adfdb2f2a488b8752489fa7bfe9039fa9ae8 Mon Sep 17 00:00:00 2001 From: Blaine Carter-Date: Fri, 20 Apr 2018 13:32:12 -0600 Subject: [PATCH 09/18] fixed to create a pool instead of a regular connection. --- samples/tutorial/connect_pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/tutorial/connect_pool.py b/samples/tutorial/connect_pool.py index baed93cf..b4c5bc3d 100644 --- a/samples/tutorial/connect_pool.py +++ b/samples/tutorial/connect_pool.py @@ -12,7 +12,7 @@ import threading import db_config -con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn, +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, min = 2, max = 5, increment = 1, threaded = True) def Query(): From cecd7d4124ef18e461b75b414f761f2c86820668 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 14:08:31 -0600 Subject: [PATCH 10/18] added connect line --- samples/tutorial/bind_insert.sql | 1 - samples/tutorial/plsql_func.sql | 1 + samples/tutorial/plsql_proc.sql | 1 + samples/tutorial/query_arraysize.sql | 1 + 4 files changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/tutorial/bind_insert.sql b/samples/tutorial/bind_insert.sql index 008761d5..068e0880 100644 --- a/samples/tutorial/bind_insert.sql +++ b/samples/tutorial/bind_insert.sql @@ -9,7 +9,6 @@ set echo on @@db_config.sql - connect &user/&pw@&connect_string begin diff --git a/samples/tutorial/plsql_func.sql b/samples/tutorial/plsql_func.sql index 2a857150..1c5c24f1 100644 --- a/samples/tutorial/plsql_func.sql +++ b/samples/tutorial/plsql_func.sql @@ -9,6 +9,7 @@ set echo on @@db_config.sql +connect &user/&pw@&connect_string begin execute immediate 'drop table ptab'; diff --git a/samples/tutorial/plsql_proc.sql b/samples/tutorial/plsql_proc.sql index 262aa1ad..34b19cf6 100644 --- a/samples/tutorial/plsql_proc.sql +++ b/samples/tutorial/plsql_proc.sql @@ -9,6 +9,7 @@ set echo on @@db_config.sql +connect &user/&pw@&connect_string create or replace procedure myproc(v1_p in number, v2_p out number) as begin diff --git a/samples/tutorial/query_arraysize.sql b/samples/tutorial/query_arraysize.sql index 3d5ec6ef..b4702145 100644 --- a/samples/tutorial/query_arraysize.sql +++ b/samples/tutorial/query_arraysize.sql @@ -9,6 +9,7 @@ set echo on @@db_config.sql +connect &user/&pw@&connect_string begin execute immediate 'drop table bigtab'; From 535d9e96d4e87575c3a581baf3f2e2f49e1374dc Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 14:42:40 -0600 Subject: [PATCH 11/18] removed extra crlf --- samples/tutorial/bind_sdo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/tutorial/bind_sdo.py b/samples/tutorial/bind_sdo.py index 3292d23b..164ca7c6 100644 --- a/samples/tutorial/bind_sdo.py +++ b/samples/tutorial/bind_sdo.py @@ -12,7 +12,6 @@ import db_config con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) - cur = con.cursor() # Create table From bd6e2c1f0d129004c4efee1e3ec5e272fa2585a9 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 14:43:15 -0600 Subject: [PATCH 12/18] Cleaned up text, fixed some errors, fixed table of contents --- ...Database-12c-Scripting-for-the-Future.html | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html index 4b1d2619..88dda247 100644 --- a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html +++ b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html @@ -17,16 +17,18 @@ Contents
- Preface
+- Connection Information
- Overview
- Using Python cx_Oracle 6 with Oracle Database 12c
- 1. Connecting to Oracle
-
- 1.1 Creating a basic connection
-- 1.2 Indentation indicates code structure
-- 1.3 Executing a query
-- 1.4 Closing connections
-- 1.5 Checking versions
+- 1.1 Set the connection values
+- 1.2 Creating a basic connection
+- 1.3 Indentation indicates code structure
+- 1.4 Executing a query
+- 1.5 Closing connections
+- 1.6 Checking versions
- 2. Connection Pooling @@ -123,7 +125,7 @@
Preface
sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamplesConnection Information
+Connection Information
The database connection information is set in two files:
@@ -333,7 +335,9 @@
1.5 Closing connections
python query.py-This gives the error "cx_Oracle.DatabaseError: DPI-1054: connection cannot be closed when open statements or LOBs exist".
+If you are using cx_Oracle 6.2+ the connection will close and you can skip ahead to section 1.6.
+ +If you are using an older version of cx_Oracle, this gives the error "cx_Oracle.DatabaseError: DPI-1054: connection cannot be closed when open statements or LOBs exist".
To fix this, edit the file and close the cursor:
@@ -362,7 +366,7 @@1.5 Closing connections
- -
1.5 Checking versions
+1.6 Checking versions
Review the code contained in
@@ -628,7 +632,7 @@versions.py
:2.4 Session pooling and DRCP
import cx_Oracle import threading -pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled", min = 2, max = 5, increment = 1, threaded = True) def Query(): @@ -1850,6 +1854,7 @@7.2 Fetching a CLOB as a string
import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): @@ -1894,6 +1899,7 @@7.2 Fetching a CLOB as a string
import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): From 720989dd2a0d398545e5d341a01779c809cd5f91 Mon Sep 17 00:00:00 2001 From: Blaine CarterDate: Fri, 20 Apr 2018 15:36:04 -0600 Subject: [PATCH 13/18] removed generatemany file. Fixed indent in connect_pool2. Fixed user in sampleenv. --- samples/tutorial/connect_pool2.py | 2 +- samples/tutorial/db_config.py | 6 +- samples/tutorial/sql/GenerateManySamples.sql | 83 -------------------- samples/tutorial/sql/SampleEnv.sql | 2 +- 4 files changed, 5 insertions(+), 88 deletions(-) delete mode 100644 samples/tutorial/sql/GenerateManySamples.sql diff --git a/samples/tutorial/connect_pool2.py b/samples/tutorial/connect_pool2.py index 8e0034ae..8abddbf8 100644 --- a/samples/tutorial/connect_pool2.py +++ b/samples/tutorial/connect_pool2.py @@ -13,7 +13,7 @@ import db_config pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn, - min = 2, max = 5, increment = 1, threaded = True) + min = 2, max = 5, increment = 1, threaded = True) def Query(): con = pool.acquire() diff --git a/samples/tutorial/db_config.py b/samples/tutorial/db_config.py index 96fbcde3..8e785678 100644 --- a/samples/tutorial/db_config.py +++ b/samples/tutorial/db_config.py @@ -1,3 +1,3 @@ -user="pythonhol" -pw="welcome" -dsn="localhost/orclpdb" +user = "pythonhol" +pw = "welcome" +dsn = "localhost/orclpdb" diff --git a/samples/tutorial/sql/GenerateManySamples.sql b/samples/tutorial/sql/GenerateManySamples.sql deleted file mode 100644 index 543d0af9..00000000 --- a/samples/tutorial/sql/GenerateManySamples.sql +++ /dev/null @@ -1,83 +0,0 @@ -set serveroutput on; - -declare - v_base_user varchar2(100) := 'pythonhol'; - v_user varchar2(100); - v_pw varchar2(100) := 'welcome'; - v_count number := 50; -begin ---Drop Samples - begin - for r in (select username - from dba_users - where username like ( upper(v_base_user) || '\_%' ) ESCAPE '\') loop - execute immediate 'drop user ' || r.username || ' cascade'; - dbms_output.put_line(r.username || ' dropped'); - end loop; - end; - --- Setup Samples - - for i in 1..v_count loop - v_user := v_base_user || '_' || i; - - execute immediate 'alter session set nls_date_format = ''YYYY-MM-DD HH24:MI:SS'''; - execute immediate 'alter session set nls_numeric_characters=''.,'''; - execute immediate 'create user ' || v_user || ' identified by ' || v_pw || ' quota unlimited on users default tablespace users'; - execute immediate 'grant - create session, - create table, - create procedure, - create type, - select any dictionary, - change notification to '|| v_user; - - execute immediate 'grant execute on dbms_aqadm to ' || v_user; - execute immediate 'grant execute on dbms_lock to ' || v_user; - execute immediate 'create table ' || v_user || '.testclobs (id number not null, myclob clob not null)'; - --- Sequence for connect_pool.py - execute immediate 'create sequence ' || v_user || '.myseq'; - --- EMP/DEPT tables - execute immediate 'CREATE TABLE ' || v_user || '.EMP - (EMPNO NUMBER(4) NOT NULL, - ENAME VARCHAR2(10), - JOB VARCHAR2(9), - MGR NUMBER(4), - HIREDATE DATE, - SAL NUMBER(7, 2), - COMM NUMBER(7, 2), - DEPTNO NUMBER(2))'; - execute immediate 'CREATE TABLE ' || v_user || '.DEPT - (DEPTNO NUMBER(2), - DNAME VARCHAR2(14), - LOC VARCHAR2(13) )'; - - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (10, ''ACCOUNTING'', ''NEW YORK'')'; - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (20, ''RESEARCH'', ''DALLAS'')'; - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (30, ''SALES'', ''CHICAGO'')'; - execute immediate 'INSERT INTO ' || v_user || '.DEPT VALUES (40, ''OPERATIONS'', ''BOSTON'')'; - - execute immediate 'CREATE TABLE ' || v_user || '.BONUS - (ENAME VARCHAR2(10), - JOB VARCHAR2(9), - SAL NUMBER, - COMM NUMBER)'; - - execute immediate 'CREATE TABLE ' || v_user || '.SALGRADE - (GRADE NUMBER, - LOSAL NUMBER, - HISAL NUMBER)'; - - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (1, 700, 1200)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (2, 1201, 1400)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (3, 1401, 2000)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (4, 2001, 3000)'; - execute immediate 'INSERT INTO ' || v_user || '.SALGRADE VALUES (5, 3001, 9999)'; - - commit; - dbms_output.put_line(v_user || ' Created'); - end loop; - -end; diff --git a/samples/tutorial/sql/SampleEnv.sql b/samples/tutorial/sql/SampleEnv.sql index 9c8d7c98..903952a0 100644 --- a/samples/tutorial/sql/SampleEnv.sql +++ b/samples/tutorial/sql/SampleEnv.sql @@ -13,7 +13,7 @@ set echo off termout on feedback off verify off -define main_user = "pythonhol2" -- $CX_ORACLE_SAMPLES_MAIN_USER +define main_user = "pythonhol" -- $CX_ORACLE_SAMPLES_MAIN_USER define main_password = "welcome" -- $CX_ORACLE_SAMPLES_MAIN_PASSWORD prompt ************************************************************************ From d6475a5e05303f772b309b6573913c5950ee7053 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 15:40:42 -0600 Subject: [PATCH 14/18] attempting to reset the file --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 3f57d61f..08ade4eb 100644 --- a/setup.py +++ b/setup.py @@ -124,4 +124,3 @@ def run(self): keywords = "Oracle", license = "BSD License", classifiers = classifiers) - From 7ab667b8270f2d17b0d364a4034b60309265e463 Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 16:01:42 -0600 Subject: [PATCH 15/18] Revert "attempting to reset the file" This reverts commit d6475a5e05303f772b309b6573913c5950ee7053. --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 08ade4eb..3f57d61f 100644 --- a/setup.py +++ b/setup.py @@ -124,3 +124,4 @@ def run(self): keywords = "Oracle", license = "BSD License", classifiers = classifiers) + From 552b299886c55d1b01377f8ee7af01191b1014fa Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Fri, 20 Apr 2018 16:30:04 -0600 Subject: [PATCH 16/18] fixed some formatting errors and referenced the config values --- ...acle-Database-12c-Scripting-for-the-Future.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html index 88dda247..bbb7ae54 100644 --- a/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html +++ b/samples/tutorial/Python-and-Oracle-Database-12c-Scripting-for-the-Future.html @@ -176,9 +176,9 @@ 1.1 Set the connection values
Review the code contained in
db_config.py
anddb_config.sql
:db_config.py
-user="pythonhol" -pw="welcome" -dsn="localhost/orclpdb" +user = "pythonhol" +pw = "welcome" +dsn = "localhost/orclpdb"db_config.sql
@@ -205,9 +205,9 @@1.2 Creating a basic connection
accessing the Oracle database. Many inbuilt and third party modules can be included in this way in Python scripts. -The
connect()
method is passed the username - "pythonhol", the password "welcome" and the connection - string. In this case, Oracle's Easy Connect connection +The
@@ -452,7 +452,7 @@connect()
method is passed the username, + the password and the connection string that you configured in + the db_config.py module. In this case, Oracle's Easy Connect connection string syntax is used. It consists of the hostname of your machine,localhost
, and the database service nameorclpdb
.1.6 Checking versions
The
SessionPool()
function creates a pool of - Oracle "sessions" for the "pythonhol" user. Sessions in the pool + Oracle "sessions" for the user. Sessions in the pool can be used by cx_Oracle connections by callingpool.acquire()
. The initial pool size is 2 sessions. The maximum size is 5 sessions. When the pool needs to grow, 1 new From 51365d179ec691b413f442d99b65ef0e7610bbc3 Mon Sep 17 00:00:00 2001 From: Blaine CarterDate: Fri, 20 Apr 2018 22:12:16 -0600 Subject: [PATCH 17/18] added db_config and made changes to solutions to use db_config values --- samples/tutorial/solutions/aq-dequeue.py | 3 ++- samples/tutorial/solutions/aq-enqueue.py | 3 ++- samples/tutorial/solutions/aq-queuestart.py | 3 ++- samples/tutorial/solutions/bind_insert.py | 3 ++- samples/tutorial/solutions/bind_sdo.py | 3 ++- samples/tutorial/solutions/connect_pool2.py | 3 ++- samples/tutorial/solutions/db_config.py | 7 +++++++ samples/tutorial/solutions/query-2.py | 3 ++- samples/tutorial/solutions/query.py | 3 ++- samples/tutorial/solutions/query_many.py | 3 ++- samples/tutorial/solutions/rowfactory.py | 3 ++- samples/tutorial/solutions/subclass.py | 3 ++- samples/tutorial/solutions/type_converter.py | 3 ++- samples/tutorial/solutions/type_output.py | 3 ++- samples/tutorial/solutions/versions.py | 3 ++- 15 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 samples/tutorial/solutions/db_config.py diff --git a/samples/tutorial/solutions/aq-dequeue.py b/samples/tutorial/solutions/aq-dequeue.py index b4a8b612..f9ce2a5a 100644 --- a/samples/tutorial/solutions/aq-dequeue.py +++ b/samples/tutorial/solutions/aq-dequeue.py @@ -10,8 +10,9 @@ import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/solutions/aq-enqueue.py b/samples/tutorial/solutions/aq-enqueue.py index cf9d50ac..49a12b5c 100644 --- a/samples/tutorial/solutions/aq-enqueue.py +++ b/samples/tutorial/solutions/aq-enqueue.py @@ -10,8 +10,9 @@ import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/solutions/aq-queuestart.py b/samples/tutorial/solutions/aq-queuestart.py index 93dd8d01..4ab2a11f 100644 --- a/samples/tutorial/solutions/aq-queuestart.py +++ b/samples/tutorial/solutions/aq-queuestart.py @@ -10,8 +10,9 @@ import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() BOOK_TYPE_NAME = "UDT_BOOK" diff --git a/samples/tutorial/solutions/bind_insert.py b/samples/tutorial/solutions/bind_insert.py index 8df69993..c4e5c109 100644 --- a/samples/tutorial/solutions/bind_insert.py +++ b/samples/tutorial/solutions/bind_insert.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() rows = [ (1, "First" ), (2, "Second" ), diff --git a/samples/tutorial/solutions/bind_sdo.py b/samples/tutorial/solutions/bind_sdo.py index 18e3ee16..4f21eda0 100644 --- a/samples/tutorial/solutions/bind_sdo.py +++ b/samples/tutorial/solutions/bind_sdo.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() # Create table diff --git a/samples/tutorial/solutions/connect_pool2.py b/samples/tutorial/solutions/connect_pool2.py index 73f9fbd4..5fc47431 100644 --- a/samples/tutorial/solutions/connect_pool2.py +++ b/samples/tutorial/solutions/connect_pool2.py @@ -11,8 +11,9 @@ import cx_Oracle import threading import time +import db_config -pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb:pooled", +pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled", min = 2, max = 5, increment = 1, threaded = True) def Query(): diff --git a/samples/tutorial/solutions/db_config.py b/samples/tutorial/solutions/db_config.py new file mode 100644 index 00000000..1fbcf941 --- /dev/null +++ b/samples/tutorial/solutions/db_config.py @@ -0,0 +1,7 @@ +import os +exec(open(os.path.join("..", "db_config.py"), "r").read()) + +import db_config as root_db_config +user = root_db_config.user +pw = root_db_config.pw +dsn = root_db_config.dsn diff --git a/samples/tutorial/solutions/query-2.py b/samples/tutorial/solutions/query-2.py index 2177d956..64d565d9 100644 --- a/samples/tutorial/solutions/query-2.py +++ b/samples/tutorial/solutions/query-2.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/solutions/query.py b/samples/tutorial/solutions/query.py index 802afba8..89881c98 100644 --- a/samples/tutorial/solutions/query.py +++ b/samples/tutorial/solutions/query.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/solutions/query_many.py b/samples/tutorial/solutions/query_many.py index d252def2..e9071f22 100644 --- a/samples/tutorial/solutions/query_many.py +++ b/samples/tutorial/solutions/query_many.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() cur.execute("select * from dept order by deptno") diff --git a/samples/tutorial/solutions/rowfactory.py b/samples/tutorial/solutions/rowfactory.py index 2968f38c..729475eb 100644 --- a/samples/tutorial/solutions/rowfactory.py +++ b/samples/tutorial/solutions/rowfactory.py @@ -10,8 +10,9 @@ import collections import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() diff --git a/samples/tutorial/solutions/subclass.py b/samples/tutorial/solutions/subclass.py index 27fa0211..61909cca 100644 --- a/samples/tutorial/solutions/subclass.py +++ b/samples/tutorial/solutions/subclass.py @@ -9,12 +9,13 @@ from __future__ import print_function import cx_Oracle +import db_config class MyConnection(cx_Oracle.Connection): def __init__(self): print("Connecting to database") - return super(MyConnection, self).__init__("pythonhol", "welcome", "localhost/orclpdb") + return super(MyConnection, self).__init__(db_config.user, db_config.pw, db_config.dsn) def cursor(self): return MyCursor(self) diff --git a/samples/tutorial/solutions/type_converter.py b/samples/tutorial/solutions/type_converter.py index f9842997..1bd20fc1 100644 --- a/samples/tutorial/solutions/type_converter.py +++ b/samples/tutorial/solutions/type_converter.py @@ -10,8 +10,9 @@ import cx_Oracle import decimal +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale): diff --git a/samples/tutorial/solutions/type_output.py b/samples/tutorial/solutions/type_output.py index 0e3a449e..3f56e16f 100644 --- a/samples/tutorial/solutions/type_output.py +++ b/samples/tutorial/solutions/type_output.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) cur = con.cursor() diff --git a/samples/tutorial/solutions/versions.py b/samples/tutorial/solutions/versions.py index 572ece27..513ac30c 100644 --- a/samples/tutorial/solutions/versions.py +++ b/samples/tutorial/solutions/versions.py @@ -9,8 +9,9 @@ from __future__ import print_function import cx_Oracle +import db_config -con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb") +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) print(cx_Oracle.version) print("Database version:", con.version) From 215998c275b153ddab4edbef6adfc3ed704d5c0c Mon Sep 17 00:00:00 2001 From: Blaine Carter Date: Sat, 21 Apr 2018 10:20:52 -0600 Subject: [PATCH 18/18] removed extra variables --- samples/tutorial/solutions/db_config.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/samples/tutorial/solutions/db_config.py b/samples/tutorial/solutions/db_config.py index 1fbcf941..375f6cc1 100644 --- a/samples/tutorial/solutions/db_config.py +++ b/samples/tutorial/solutions/db_config.py @@ -1,7 +1,4 @@ import os -exec(open(os.path.join("..", "db_config.py"), "r").read()) -import db_config as root_db_config -user = root_db_config.user -pw = root_db_config.pw -dsn = root_db_config.dsn +dirName = os.path.dirname(os.path.dirname(__file__)) +exec(open(os.path.join(dirName, "db_config.py"), "r").read())