[DB-SIG] oracledb question

Thomas E. Milliman Thomas.Milliman@unh.edu (Thomas E. Milliman)
Wed, 17 Sep 1997 10:45:01 -0400 (EDT)


Hi,

I'm a newcomer to python and have just succeeded in writing my first
script using the oracledb extensions.  I was a little bit surprised at
the lack of samples in the distribution and by the fact that the
documentation was rather old.  Is there any development still going on
with this stuff?

I managed to get some test scripts going by looking at mailing-list
archives and would like to contribute these (very simple) samples but
am not sure where to send them.  I'm would also guess that a "real"
python user could clean them up considerably.  I'm including them in
this message and hope they might be useful for other newbies like me.

Tom

--------------------------begin sample1.py-------------------------------
#!/usr/local/bin/python
#
# test of connecting to oracle database
#
# load oracledb module
#
import oracledb
#
# create open database connection
# NOTE: change "dbinstance" to database instance name
#
dbc=oracledb.oracledb("scott/tiger@dbinstance")
#
# create cursor object
#
cursor=dbc.cursor()
#
# operate on cursor with "execute" method
#
cursor.execute("select ename,empno from emp")
#
# fetch and print results
#
for row in cursor.fetchall():
    #
    (name,empno) = row
    # 
    # print results
    #
    print name, empno
    #
#
# close cursor
#
cursor.close()
#
# all done
#
----------------------------end sample1.py-------------------------------

--------------------------begin sample2.py-------------------------------
#!/usr/local/bin/python
#
# test of connecting to oracle database
#
# load sys module for getting command line args
#
import sys
#
# get employee name as argument
#
ename=sys.argv[1]
#
# place ename in tuple
#
name=(ename,)
#
# load oracledb module
#
import oracledb
#
# create open database connection
# NOTE: change "dbinstance" to database instance name
#
dbc=oracledb.oracledb("scott/tiger@dbinstance")
#
# create cursor object
#
cursor=dbc.cursor()
#
# select statement
#
select="select * from emp where ename = :1"
#
# operate on cursor with "execute" method to preform substitutions
#
cursor.execute(select,name)
#
# fetch results
#
row=cursor.fetchone()
#
(empno,ename,job,mgr,hiredate,sal,comm,deptno) = row
# 
# print results
#
print ename, empno, job, mgr, sal, comm, deptno
#
#
# close cursor
#
cursor.close()
#
# all done
#
----------------------------end sample2.py-------------------------------

+---------------------------------------------------------------------+
| Thomas E. Milliman                 e-mail: Thomas.Milliman@unh.edu  |
| Space Science Center                Voice: (603) 862-2867           |
| University of New Hampshire           Fax: (603) 862-4685           |
| Morse Hall, Rm 309                                                  |
| Durham, NH 03824-3525                                               |
+---------------------------------------------------------------------+

_______________
DB-SIG  - SIG on Tabular Databases in Python

send messages to: db-sig@python.org
administrivia to: db-sig-request@python.org
_______________