python-cx_Oracle/samples/RowsAsInstance.py
Anthony Tuininga 4185d96682 Adjusted samples to be clearer and to match the tables used in the test suite;
added new samples for DRCP and input/output type handlers.
2017-01-13 10:47:20 -07:00

49 lines
1.5 KiB
Python

#------------------------------------------------------------------------------
# RowsAsInstance.py
# Returns rows as instances instead of tuples. See the ceDatabase.Row class
# in the cx_PyGenLib project (http://cx-pygenlib.sourceforge.net) for a more
# advanced example.
#------------------------------------------------------------------------------
from __future__ import print_function
import cx_Oracle
class Test(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl")
cursor = connection.cursor()
# change this to False if you want to create the table yourself using SQL*Plus
# and then populate it with the data of your choice
if True:
cursor.execute("""
select count(*)
from user_tables
where table_name = 'TESTINSTANCES'""")
count, = cursor.fetchone()
if count:
cursor.execute("drop table TestInstances")
cursor.execute("""
create table TestInstances (
a varchar2(60) not null,
b number(9) not null,
c date not null
)""")
cursor.execute("insert into TestInstances values ('First', 5, sysdate)")
cursor.execute("insert into TestInstances values ('Second', 25, sysdate)")
connection.commit()
# retrieve the data and display it
cursor.execute("select * from TestInstances")
cursor.rowfactory = Test
print("Rows:")
for row in cursor:
print("a = %s, b = %s, c = %s" % (row.a, row.b, row.c))