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:
parent
4d37c85974
commit
bae5cbe76a
@ -17,16 +17,18 @@
|
||||
|
||||
<ul>
|
||||
<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="#lab" >Using Python cx_Oracle 6 with Oracle Database 12c</a></li>
|
||||
<ul>
|
||||
<li><a href="#connecting">1. Connecting to Oracle</a>
|
||||
<ul>
|
||||
<li>1.1 Creating a basic connection</li>
|
||||
<li>1.2 Indentation indicates code structure</li>
|
||||
<li>1.3 Executing a query</li>
|
||||
<li>1.4 Closing connections</li>
|
||||
<li>1.5 Checking versions</li>
|
||||
<li>1.1 Set the connection values</li>
|
||||
<li>1.2 Creating a basic connection</li>
|
||||
<li>1.3 Indentation indicates code structure</li>
|
||||
<li>1.4 Executing a query</li>
|
||||
<li>1.5 Closing connections</li>
|
||||
<li>1.6 Checking versions</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#pooling">2. Connection Pooling</a>
|
||||
@ -103,25 +105,43 @@
|
||||
preconfigured with Oracle Database, Python 3.6, cx_Oracle 6.0, a
|
||||
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>
|
||||
<li>Python 3.6</li>
|
||||
<li>cx_Oracle 6.0</li>
|
||||
<li>Oracle Database 12c</li>
|
||||
</ul>
|
||||
<ol>
|
||||
<li><a target="_blank" href="https://www.python.org/">Python</a> (3.6 preferred but 2.7 should work)</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)
|
||||
<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
|
||||
'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>
|
||||
<p>To create the schema run:</p>
|
||||
|
||||
<pre>
|
||||
sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples
|
||||
</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
|
||||
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> 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:
|
||||
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>
|
||||
<ul>
|
||||
<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>
|
||||
<pre>
|
||||
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)
|
||||
</pre>
|
||||
|
||||
@ -168,9 +205,9 @@ print("Database version:", con.version)
|
||||
accessing the Oracle database. Many inbuilt and third party
|
||||
modules can be included in this way in Python scripts.</p>
|
||||
|
||||
<p> The <code>connect()</code> method is passed the username
|
||||
"pythonhol", the password "welcome" and the connection
|
||||
string. In this case, Oracle's Easy Connect connection
|
||||
<p> The <code>connect()</code> 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, <code>localhost</code>, and the database service name
|
||||
<code>orclpdb</code>. </p>
|
||||
@ -192,7 +229,7 @@ print("Database version:", con.version)
|
||||
</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
|
||||
or braces to indicate blocks of code.</p>
|
||||
@ -202,7 +239,9 @@ print("Database version:", con.version)
|
||||
|
||||
<pre>
|
||||
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)
|
||||
</pre>
|
||||
|
||||
@ -224,20 +263,24 @@ con = cx_Oracle.connect("pythonhol", "welcome", "localhost/orclpdb")
|
||||
</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>
|
||||
|
||||
<pre>
|
||||
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>
|
||||
|
||||
<p>Edit the file and add the code shown in bold below:</p>
|
||||
|
||||
<pre>
|
||||
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()
|
||||
cur.execute("select * from dept order by deptno")
|
||||
@ -261,7 +304,7 @@ for row in res:
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<h4>1.4 Closing connections</h4>
|
||||
<h4>1.5 Closing connections</h4>
|
||||
|
||||
<p>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 @@ for row in res:
|
||||
|
||||
<pre>
|
||||
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 @@ for row in res:
|
||||
|
||||
<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>
|
||||
|
||||
<pre>
|
||||
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 @@ con.close()
|
||||
</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>
|
||||
|
||||
<pre>
|
||||
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>
|
||||
|
||||
@ -337,7 +388,9 @@ print(cx_Oracle.version)</pre>
|
||||
|
||||
<pre>
|
||||
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)
|
||||
<strong>print("Database version:", con.version)
|
||||
@ -373,8 +426,9 @@ Client version: (12, 2, 0, 1, 0)
|
||||
<pre>
|
||||
import cx_Oracle
|
||||
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)
|
||||
|
||||
def Query():
|
||||
@ -398,7 +452,7 @@ print("All done!")
|
||||
</pre>
|
||||
|
||||
<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
|
||||
<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
|
||||
@ -445,8 +499,9 @@ of threads:</p>
|
||||
<pre>
|
||||
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 @@ is available.</p>
|
||||
|
||||
<pre>
|
||||
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="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF)
|
||||
print("Database version:", con.version)
|
||||
</pre>
|
||||
@ -575,7 +632,7 @@ print("Database version:", con.version)
|
||||
import cx_Oracle
|
||||
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)
|
||||
|
||||
def Query():
|
||||
@ -658,7 +715,9 @@ import time</pre>
|
||||
|
||||
<pre>
|
||||
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 @@ for deptno, dname, loc in cur:
|
||||
|
||||
<pre>
|
||||
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 @@ print(row)
|
||||
|
||||
<pre>
|
||||
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 @@ print(res)
|
||||
|
||||
<pre>
|
||||
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 @@ commit;
|
||||
<pre>
|
||||
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 @@ print(elapsed, "seconds")
|
||||
|
||||
<pre>
|
||||
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 @@ create table mytab (id number, data varchar2(20), constraint my_pk primary key (
|
||||
|
||||
<pre>
|
||||
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 @@ rows = [ (1, "First" ), (2, "Second" ),
|
||||
|
||||
<pre>
|
||||
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 @@ print(res)
|
||||
Spatial Data Objects (SDO).</p>
|
||||
|
||||
<p>In a terminal window, start SQL*Plus:</p>
|
||||
<p>(Modify the following command to use your connection values.)</p>
|
||||
|
||||
<pre>
|
||||
sqlplus pythonhol/welcome@localhost/orclpdb
|
||||
@ -1094,8 +1161,9 @@ desc MDSYS.SDO_GEOMETRY
|
||||
|
||||
<pre>
|
||||
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 @@ create or replace function myfunc(d_p in varchar2, i_p in number) return number
|
||||
|
||||
<pre>
|
||||
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 @@ end;
|
||||
|
||||
<pre>
|
||||
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 @@ print(myvar.getvalue())
|
||||
|
||||
<pre>
|
||||
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 @@ for row in cur.execute("select * from dept"):
|
||||
|
||||
<pre>
|
||||
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 @@ for value, in cur.execute("select 0.1 from dual"):
|
||||
<pre>
|
||||
import cx_Oracle
|
||||
<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()
|
||||
|
||||
<strong>def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale):
|
||||
@ -1481,8 +1554,9 @@ def ReturnNumbersAsDecimal(cursor, name, defaultType, size, precision, scale):
|
||||
|
||||
<pre>
|
||||
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 @@ an Oracle object from the Python object values. The
|
||||
|
||||
<pre>
|
||||
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 @@ print("CLOB data:", clobdata)
|
||||
|
||||
<pre>
|
||||
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 @@ print("CLOB data:", clobdata)</strong>
|
||||
<pre>
|
||||
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 @@ for row in rows:
|
||||
|
||||
<pre>
|
||||
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 @@ print("Number of rows:", count)
|
||||
|
||||
<pre>
|
||||
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)
|
||||
|
||||
<strong> def cursor(self):
|
||||
return MyCursor(self)
|
||||
@ -1879,8 +1958,9 @@ invoke the parent methods to do the actual statement execution.</p>
|
||||
<pre>
|
||||
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"
|
||||
|
||||
@ -10,8 +10,9 @@ from __future__ import print_function
|
||||
|
||||
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"
|
||||
|
||||
@ -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" ),
|
||||
|
||||
@ -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));
|
||||
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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...")
|
||||
|
||||
@ -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...")
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -10,8 +10,9 @@ from __future__ import print_function
|
||||
|
||||
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():
|
||||
|
||||
@ -10,8 +10,9 @@ from __future__ import print_function
|
||||
|
||||
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():
|
||||
|
||||
3
samples/tutorial/db_config.py
Normal file
3
samples/tutorial/db_config.py
Normal file
@ -0,0 +1,3 @@
|
||||
user = "pythonhol"
|
||||
pw = "welcome"
|
||||
dsn = "localhost/orclpdb"
|
||||
3
samples/tutorial/db_config.sql
Normal file
3
samples/tutorial/db_config.sql
Normal file
@ -0,0 +1,3 @@
|
||||
def user = "pythonhol"
|
||||
def pw = "welcome"
|
||||
def connect_string = "localhost/orclpdb"
|
||||
@ -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))
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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')
|
||||
|
||||
@ -10,8 +10,9 @@ from __future__ import print_function
|
||||
|
||||
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()
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -10,8 +10,9 @@ from __future__ import print_function
|
||||
|
||||
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")
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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"):
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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...")
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user