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..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 @@ -17,16 +17,18 @@
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 +158,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 +172,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)@@ -168,9 +205,9 @@
The connect()
method is passed the username
- "pythonhol", the password "welcome" and the connection
- string. In this case, Oracle's Easy Connect connection
+
The 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 name
orclpdb
.
There are no statement terminators or begin/end keywords or braces to indicate blocks of code.
@@ -202,7 +239,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 +263,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 +304,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 +318,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") @@ -290,13 +335,17 @@1.4 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:
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") @@ -317,13 +366,15 @@1.4 Closing connections
Review the code contained in versions.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(cx_Oracle.version)@@ -337,7 +388,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(cx_Oracle.version) print("Database version:", con.version) @@ -373,8 +426,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(): @@ -398,7 +452,7 @@1.5 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 @@ -445,8 +499,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 +588,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 +632,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 +715,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 +750,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 +780,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 +822,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 +888,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 +961,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 +1016,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 +1083,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 +1133,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 +1161,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 +1325,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 +1371,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 +1420,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 +1478,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 +1501,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 +1554,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 +1674,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 +1723,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 +1780,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") @@ -1777,12 +1854,13 @@7.2 Fetching a CLOB as a string
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() @@ -1821,12 +1899,13 @@7.2 Fetching a CLOB as a string
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) @@ -1879,8 +1958,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..068e0880 100644 --- a/samples/tutorial/bind_insert.sql +++ b/samples/tutorial/bind_insert.sql @@ -8,9 +8,18 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql +connect &user/&pw@&connect_string -drop table mytab; +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..164ca7c6 100644 --- a/samples/tutorial/bind_sdo.py +++ b/samples/tutorial/bind_sdo.py @@ -9,8 +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() # Create table 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..b4c5bc3d 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", +pool = cx_Oracle.SessionPool(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..8abddbf8 100644 --- a/samples/tutorial/connect_pool2.py +++ b/samples/tutorial/connect_pool2.py @@ -10,8 +10,9 @@ 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(): diff --git a/samples/tutorial/db_config.py b/samples/tutorial/db_config.py new file mode 100644 index 00000000..8e785678 --- /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..1c5c24f1 100644 --- a/samples/tutorial/plsql_func.sql +++ b/samples/tutorial/plsql_func.sql @@ -8,7 +8,18 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql +connect &user/&pw@&connect_string + +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..34b19cf6 100644 --- a/samples/tutorial/plsql_proc.sql +++ b/samples/tutorial/plsql_proc.sql @@ -8,7 +8,8 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@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.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..b4702145 100644 --- a/samples/tutorial/query_arraysize.sql +++ b/samples/tutorial/query_arraysize.sql @@ -8,9 +8,18 @@ set echo on -connect pythonhol/welcome@localhost/orclpdb +@@db_config.sql +connect &user/&pw@&connect_string -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/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..375f6cc1 --- /dev/null +++ b/samples/tutorial/solutions/db_config.py @@ -0,0 +1,4 @@ +import os + +dirName = os.path.dirname(os.path.dirname(__file__)) +exec(open(os.path.join(dirName, "db_config.py"), "r").read()) 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) 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)