Modified the HoL for Collaborate (#169)

Modified samples and instructions to import common connection information from db_config.py or db_config.sql in order to make setup a bit more generic.
This commit is contained in:
Blaine Carter 2018-04-20 16:35:17 -06:00 committed by Anthony Tuininga
parent 4d37c85974
commit bae5cbe76a
31 changed files with 234 additions and 94 deletions

View File

@ -17,16 +17,18 @@
<ul> <ul>
<li><a href="#preface" >Preface</a></li> <li><a href="#preface" >Preface</a></li>
<li><a href="#connectioninformation" >Connection Information</a></li>
<li><a href="#overview" >Overview</a></li> <li><a href="#overview" >Overview</a></li>
<li><a href="#lab" >Using Python cx_Oracle 6 with Oracle Database 12c</a></li> <li><a href="#lab" >Using Python cx_Oracle 6 with Oracle Database 12c</a></li>
<ul> <ul>
<li><a href="#connecting">1. Connecting to Oracle</a> <li><a href="#connecting">1. Connecting to Oracle</a>
<ul> <ul>
<li>1.1 Creating a basic connection</li> <li>1.1 Set the connection values</li>
<li>1.2 Indentation indicates code structure</li> <li>1.2 Creating a basic connection</li>
<li>1.3 Executing a query</li> <li>1.3 Indentation indicates code structure</li>
<li>1.4 Closing connections</li> <li>1.4 Executing a query</li>
<li>1.5 Checking versions</li> <li>1.5 Closing connections</li>
<li>1.6 Checking versions</li>
</ul> </ul>
</li> </li>
<li><a href="#pooling">2. Connection Pooling</a> <li><a href="#pooling">2. Connection Pooling</a>
@ -103,25 +105,43 @@
preconfigured with Oracle Database, Python 3.6, cx_Oracle 6.0, a preconfigured with Oracle Database, Python 3.6, cx_Oracle 6.0, a
text editor, and access to a terminal console.</p> text editor, and access to a terminal console.</p>
<p>To set up a similar environment yourself, install:</p> <p>To set up a similar environment yourself, install the following required software:</p>
<ul> <ol>
<li>Python 3.6</li> <li><a target="_blank" href="https://www.python.org/">Python</a> (3.6 preferred but 2.7 should work)</li>
<li>cx_Oracle 6.0</li> <li>cx_Oracle (6.2.1 preferred any 6.x should work) and Oracle Instant Client Package - Basic (12.2 preferred 12.1 should work)
<li>Oracle Database 12c</li> <ul>
</ul> <li><a target="_blank" href="http://cx-oracle.readthedocs.io/en/latest/installation.html#installing-cx-oracle-on-linux">Linux</a></li>
<li><a target="_blank" href="http://cx-oracle.readthedocs.io/en/latest/installation.html#installing-cx-oracle-on-macos">macOS</a> - please note the special instructions for macOS in the link.</li>
<li><a target="_blank" href="http://cx-oracle.readthedocs.io/en/latest/installation.html#installing-cx-oracle-on-windows">Windows</a></li>
</ul>
</li>
<li>Oracle <a target="_blank" href="http://www.oracle.com/technetwork/database/database-technologies/instant-client/overview/index.html">Instant Client Package - SQL*Plus</a> OR Oracle <a target="_blank" href="http://www.oracle.com/technetwork/developer-tools/sqlcl/downloads/index.html">SQLcl</a></li>
</ol>
<p>It is easist to have a local pluggable database with the service <p>To create the schema run:</p>
'orclpdb' configured. If your database is not local, or has a
different service, you will need to modify the connect string in all
samples.</p>
<p>To create the schema run:</p>
<pre> <pre>
sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples
</pre> </pre>
<h2><a name="connectioninformation">Connection Information</a></h2>
<p>The database connection information is set in two files:
<ul>
<li>db_config.py which is imported by the other Python modules.</li>
<li>db_config.sql which is used by the other SQL scripts.</li>
</ul>
</p>
<p>The username is "pythonhol" with
the password "welcome". The connect string is "localhost/orclpdb".
See <code>sql/SampleEnv.sql</code>.</p>
<p>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.</p>
<p>The following sections may need adjusting, depending on how you <p>The following sections may need adjusting, depending on how you
have set up your environment.</p> have set up your environment.</p>
@ -138,10 +158,6 @@ sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples
<p>Use the Desktop icons to start editors and terminal windows.</p> <p>Use the Desktop icons to start editors and terminal windows.</p>
<p> A pre-created schema exists. The username is "pythonhol" with
the password "welcome". The connect string is "localhost/orclpdb".
See <code>sql/SampleEnv.sql</code>.</p>
<p>If you are new to Python review the <a href="#primer">Appendix: <p>If you are new to Python review the <a href="#primer">Appendix:
Python Primer</a> to gain an understanding of the language. </p> Python Primer</a> to gain an understanding of the language. </p>
@ -156,11 +172,32 @@ sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples
<h3><a name="connecting">1. Connecting to Oracle</a></h3> <h3><a name="connecting">1. Connecting to Oracle</a></h3>
<ul> <ul>
<li> <li>
<h4>1.1 Creating a basic connection</h4> <h4>1.1 Set the connection values</h4>
<p>Review the code contained in <code>db_config.py</code> and <code>db_config.sql</code>:</p>
<code>db_config.py</code>
<pre>
user = "pythonhol"
pw = "welcome"
dsn = "localhost/orclpdb"
</pre>
<code>db_config.sql</code>
<pre>
def user = "pythonhol"
def pw = "welcome"
def connect_string = "localhost/orclpdb"
</pre>
<p>Modify the values in both files to match the connection information for your environment.</p>
</li>
<li>
<h4>1.2 Creating a basic connection</h4>
<p>Review the code contained in <code>connect.py</code>:</p> <p>Review the code contained in <code>connect.py</code>:</p>
<pre> <pre>
import cx_Oracle 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) print("Database version:", con.version)
</pre> </pre>
@ -168,9 +205,9 @@ print("Database version:", con.version)
accessing the Oracle database. Many inbuilt and third party accessing the Oracle database. Many inbuilt and third party
modules can be included in this way in Python scripts.</p> modules can be included in this way in Python scripts.</p>
<p> The <code>connect()</code> method is passed the username <p> The <code>connect()</code> method is passed the username,
"pythonhol", the password "welcome" and the connection the password and the connection string that you configured in
string. In this case, Oracle's Easy Connect connection the db_config.py module. In this case, Oracle's Easy Connect connection
string syntax is used. It consists of the hostname of your string syntax is used. It consists of the hostname of your
machine, <code>localhost</code>, and the database service name machine, <code>localhost</code>, and the database service name
<code>orclpdb</code>. </p> <code>orclpdb</code>. </p>
@ -192,7 +229,7 @@ print("Database version:", con.version)
</li> </li>
<li> <li>
<h4>1.2 Indentation indicates code structure</h4> <h4>1.3 Indentation indicates code structure</h4>
<p>There are no statement terminators or begin/end keywords <p>There are no statement terminators or begin/end keywords
or braces to indicate blocks of code.</p> or braces to indicate blocks of code.</p>
@ -202,7 +239,9 @@ print("Database version:", con.version)
<pre> <pre>
import cx_Oracle 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) print("Database version:", con.version)
</pre> </pre>
@ -224,20 +263,24 @@ con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb")
</li> </li>
<li> <li>
<h4>1.3 Executing a query</h4> <h4>1.4 Executing a query</h4>
<p>Open <code>query.py</code> in an editor. It looks like:</p> <p>Open <code>query.py</code> in an editor. It looks like:</p>
<pre> <pre>
import cx_Oracle 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)
</pre> </pre>
<p>Edit the file and add the code shown in bold below:</p> <p>Edit the file and add the code shown in bold below:</p>
<pre> <pre>
import cx_Oracle 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)
<strong>cur = con.cursor() <strong>cur = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -261,7 +304,7 @@ for row in res:
</li> </li>
<li> <li>
<h4>1.4 Closing connections</h4> <h4>1.5 Closing connections</h4>
<p>Connections and other resources used by cx_Oracle will <p>Connections and other resources used by cx_Oracle will
automatically be closed at the end of scope. This is a common automatically be closed at the end of scope. This is a common
@ -275,7 +318,9 @@ for row in res:
<pre> <pre>
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -290,13 +335,17 @@ for row in res:
<pre><strong>python query.py</strong></pre> <pre><strong>python query.py</strong></pre>
<p>This gives the error "cx_Oracle.DatabaseError: DPI-1054: connection cannot be closed when open statements or LOBs exist".</p> <p>If you are using cx_Oracle 6.2+ the connection will close and you can skip ahead to section 1.6.</p>
<p>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".</p>
<p>To fix this, edit the file and close the cursor:</p> <p>To fix this, edit the file and close the cursor:</p>
<pre> <pre>
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -317,13 +366,15 @@ con.close()
</li> </li>
<li> <li>
<h4>1.5 Checking versions</h4> <h4>1.6 Checking versions</h4>
<p>Review the code contained in <code>versions.py</code>:</p> <p>Review the code contained in <code>versions.py</code>:</p>
<pre> <pre>
import cx_Oracle 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)</pre> print(cx_Oracle.version)</pre>
@ -337,7 +388,9 @@ print(cx_Oracle.version)</pre>
<pre> <pre>
import cx_Oracle 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(cx_Oracle.version)
<strong>print("Database version:", con.version) <strong>print("Database version:", con.version)
@ -373,8 +426,9 @@ Client version: (12, 2, 0, 1, 0)
<pre> <pre>
import cx_Oracle import cx_Oracle
import threading import threading
import db_config
pool = cx_Oracle.<strong>SessionPool</strong>("pythonhol", "welcome", "localhost/orclpdb", pool = cx_Oracle.<strong>SessionPool</strong>(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(): def Query():
@ -398,7 +452,7 @@ print("All done!")
</pre> </pre>
<p>The <code>SessionPool()</code> function creates a pool of <p>The <code>SessionPool()</code> 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 calling can be used by cx_Oracle connections by calling
<code>pool.acquire()</code>. The initial pool size is 2 sessions. <code>pool.acquire()</code>. The initial pool size is 2 sessions.
The maximum size is 5 sessions. When the pool needs to grow, 1 new The maximum size is 5 sessions. When the pool needs to grow, 1 new
@ -445,8 +499,9 @@ of threads:</p>
<pre> <pre>
import cx_Oracle import cx_Oracle
import threading 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) min = 2, max = 5, increment = 1, threaded = True)
def Query(): def Query():
@ -533,7 +588,9 @@ is available.</p>
<pre> <pre>
import cx_Oracle import cx_Oracle
con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb<strong>:pooled</strong>", import db_config
con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn + "<strong>:pooled</strong>",
cclass=&quot;PYTHONHOL&quot;, purity=cx_Oracle.ATTR_PURITY_SELF) cclass=&quot;PYTHONHOL&quot;, purity=cx_Oracle.ATTR_PURITY_SELF)
print("Database version:", con.version) print("Database version:", con.version)
</pre> </pre>
@ -575,7 +632,7 @@ print("Database version:", con.version)
import cx_Oracle import cx_Oracle
import threading import threading
pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb<strong>:pooled</strong>", pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn <strong>+ ":pooled"</strong>,
min = 2, max = 5, increment = 1, threaded = True) min = 2, max = 5, increment = 1, threaded = True)
def Query(): def Query():
@ -658,7 +715,9 @@ import time</pre>
<pre> <pre>
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -691,8 +750,9 @@ for deptno, dname, loc in cur:
<pre> <pre>
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -720,8 +780,9 @@ print(row)
<pre> <pre>
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -761,8 +822,9 @@ print(res)
<pre> <pre>
import cx_Oracle 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 = con.cursor(scrollable = True)
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")
@ -826,8 +888,9 @@ commit;
<pre> <pre>
import cx_Oracle import cx_Oracle
import time 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() start = time.time()
@ -898,8 +961,9 @@ print(elapsed, "seconds")
<pre> <pre>
import cx_Oracle 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 = con.cursor()
cur.prepare("select * from dept where deptno = :id order by deptno") cur.prepare("select * from dept where deptno = :id order by deptno")
@ -952,8 +1016,9 @@ create table mytab (id number, data varchar2(20), constraint my_pk primary key (
<pre> <pre>
import cx_Oracle 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 = con.cursor()
rows = [ (1, &quot;First&quot; ), (2, &quot;Second&quot; ), rows = [ (1, &quot;First&quot; ), (2, &quot;Second&quot; ),
@ -1018,8 +1083,9 @@ rows = [ (1, &quot;First&quot; ), (2, &quot;Second&quot; ),
<pre> <pre>
import cx_Oracle 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 = con.cursor()
rows = [ (1, &quot;First&quot; ), (2, &quot;Second&quot; ), rows = [ (1, &quot;First&quot; ), (2, &quot;Second&quot; ),
@ -1067,6 +1133,7 @@ print(res)
Spatial Data Objects (SDO).</p> Spatial Data Objects (SDO).</p>
<p>In a terminal window, start SQL*Plus:</p> <p>In a terminal window, start SQL*Plus:</p>
<p>(Modify the following command to use your connection values.)</p>
<pre> <pre>
sqlplus pythonhol/welcome@localhost/orclpdb sqlplus pythonhol/welcome@localhost/orclpdb
@ -1094,8 +1161,9 @@ desc MDSYS.SDO_GEOMETRY
<pre> <pre>
import cx_Oracle 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 = con.cursor()
# Create table # Create table
@ -1257,8 +1325,9 @@ create or replace function myfunc(d_p in varchar2, i_p in number) return number
<pre> <pre>
import cx_Oracle 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 = con.cursor()
res = cur.callfunc('myfunc', int, ('abc', 2)) res = cur.callfunc('myfunc', int, ('abc', 2))
@ -1302,8 +1371,9 @@ end;
<pre> <pre>
import cx_Oracle 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 = con.cursor()
myvar = cur.var(int) myvar = cur.var(int)
@ -1350,8 +1420,9 @@ print(myvar.getvalue())
<pre> <pre>
import cx_Oracle 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 = con.cursor()
print("Standard output...") print("Standard output...")
@ -1407,8 +1478,9 @@ for row in cur.execute("select * from dept"):
<pre> <pre>
import cx_Oracle 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 = con.cursor()
for value, in cur.execute("select 0.1 from dual"): for value, in cur.execute("select 0.1 from dual"):
@ -1429,8 +1501,9 @@ for value, in cur.execute("select 0.1 from dual"):
<pre> <pre>
import cx_Oracle import cx_Oracle
<strong>import decimal</strong> <strong>import decimal</strong>
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 = con.cursor()
<strong>def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale): <strong>def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale):
@ -1481,8 +1554,9 @@ def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale):
<pre> <pre>
import cx_Oracle 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 = con.cursor()
# Create table # Create table
@ -1600,8 +1674,9 @@ an Oracle object from the Python object values. The
<pre> <pre>
import cx_Oracle 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 = con.cursor()
print("Inserting data...") print("Inserting data...")
@ -1648,8 +1723,9 @@ print("CLOB data:", clobdata)
<pre> <pre>
import cx_Oracle 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 = con.cursor()
print("Inserting data...") print("Inserting data...")
@ -1704,8 +1780,9 @@ print("CLOB data:", clobdata)</strong>
<pre> <pre>
import collections import collections
import cx_Oracle 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 = con.cursor()
cur.execute("select deptno, dname from dept") cur.execute("select deptno, dname from dept")
@ -1777,12 +1854,13 @@ for row in rows:
<pre> <pre>
import cx_Oracle import cx_Oracle
import db_config
class MyConnection(cx_Oracle.Connection): class MyConnection(cx_Oracle.Connection):
def __init__(self): def __init__(self):
print("Connecting to database") 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() con = MyConnection()
cur = con.cursor() cur = con.cursor()
@ -1821,12 +1899,13 @@ print("Number of rows:", count)
<pre> <pre>
import cx_Oracle import cx_Oracle
import db_config
class MyConnection(cx_Oracle.Connection): class MyConnection(cx_Oracle.Connection):
def __init__(self): def __init__(self):
print("Connecting to database") 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)
<strong> def cursor(self): <strong> def cursor(self):
return MyCursor(self) return MyCursor(self)
@ -1879,8 +1958,9 @@ invoke the parent methods to do the actual statement execution.</p>
<pre> <pre>
import cx_Oracle import cx_Oracle
import decimal 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() cur = con.cursor()
BOOK_TYPE_NAME = "UDT_BOOK" BOOK_TYPE_NAME = "UDT_BOOK"

View File

@ -10,8 +10,9 @@ from __future__ import print_function
import cx_Oracle import cx_Oracle
import decimal 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() cur = con.cursor()
BOOK_TYPE_NAME = "UDT_BOOK" BOOK_TYPE_NAME = "UDT_BOOK"

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
rows = [ (1, "First" ), (2, "Second" ), rows = [ (1, "First" ), (2, "Second" ),

View File

@ -8,9 +8,18 @@
set echo on 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)); create table mytab (id number, data varchar2(20), constraint my_pk primary key (id));

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
cur.prepare("select * from dept where deptno = :id order by deptno") cur.prepare("select * from dept where deptno = :id order by deptno")

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
# Create table # Create table

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
print("Inserting data...") print("Inserting data...")

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
print("Inserting data...") print("Inserting data...")

View File

@ -9,6 +9,7 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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) print("Database version:", con.version)

View File

@ -9,7 +9,8 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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) cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF)
print("Database version:", con.version) print("Database version:", con.version)

View File

@ -10,8 +10,9 @@ from __future__ import print_function
import cx_Oracle import cx_Oracle
import threading 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) min = 2, max = 5, increment = 1, threaded = True)
def Query(): def Query():

View File

@ -10,8 +10,9 @@ from __future__ import print_function
import cx_Oracle import cx_Oracle
import threading 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) min = 2, max = 5, increment = 1, threaded = True)
def Query(): def Query():

View File

@ -0,0 +1,3 @@
user = "pythonhol"
pw = "welcome"
dsn = "localhost/orclpdb"

View File

@ -0,0 +1,3 @@
def user = "pythonhol"
def pw = "welcome"
def connect_string = "localhost/orclpdb"

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
res = cur.callfunc('myfunc', int, ('abc', 2)) res = cur.callfunc('myfunc', int, ('abc', 2))

View File

@ -8,7 +8,18 @@
set echo on 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); create table ptab (mydata varchar(20), myid number);

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
myvar = cur.var(int) myvar = cur.var(int)

View File

@ -8,7 +8,8 @@
set echo on 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 create or replace procedure myproc(v1_p in number, v2_p out number) as
begin begin

View File

@ -9,5 +9,6 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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)

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
cur.execute('select * from dept order by deptno') cur.execute('select * from dept order by deptno')

View File

@ -10,8 +10,9 @@ from __future__ import print_function
import cx_Oracle import cx_Oracle
import time 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() start = time.time()

View File

@ -8,9 +8,18 @@
set echo on 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)); create table bigtab (mycol varchar2(20));
begin begin

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor(scrollable = True)
cur.execute("select * from dept order by deptno") cur.execute("select * from dept order by deptno")

View File

@ -10,8 +10,9 @@ from __future__ import print_function
import collections import collections
import cx_Oracle 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 = con.cursor()
cur.execute("select deptno, dname from dept") cur.execute("select deptno, dname from dept")

View File

@ -9,12 +9,13 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle import cx_Oracle
import db_config
class MyConnection(cx_Oracle.Connection): class MyConnection(cx_Oracle.Connection):
def __init__(self): def __init__(self):
print("Connecting to database") 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() con = MyConnection()
cur = con.cursor() cur = con.cursor()

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
for value, in cur.execute("select 0.1 from dual"): for value, in cur.execute("select 0.1 from dual"):

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
# Create table # Create table

View File

@ -9,8 +9,9 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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 = con.cursor()
print("Standard output...") print("Standard output...")

View File

@ -9,7 +9,8 @@
from __future__ import print_function from __future__ import print_function
import cx_Oracle 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(cx_Oracle.version)