Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting column info in cx_oracle when table is empty?

I am working on an a handler for the python logging module. That essentially logs to an oracle database.

I am using cx_oracle, and something i don't know how to get is the column values when the table is empty.

cursor.execute('select * from FOO')
for row in cursor:
    # this is never executed because cursor has no rows
    print '%s\n' % row.description

# This prints none
row = cursor.fetchone()
print str(row)

row = cursor.fetchvars
# prints useful info
for each in row:
    print each

The output is:

None
<cx_Oracle.DATETIME with value [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None
, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]>
<cx_Oracle.STRING with value [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]>

Now looking at the var data i can see the data types, and their sizes (count nones?) but thats missing the column name.

How can i go about this?

like image 419
UberJumper Avatar asked Jun 05 '09 14:06

UberJumper


1 Answers

I think the description attribute may be what you are looking for. This returns a list of tuples that describe the columns of the data returned. It works quite happily if there are no rows returned, for example:

>>> import cx_Oracle
>>> c = cx_Oracle.connect("username", "password")
>>> cr = c.cursor()
>>> cr.execute("select * from dual where 1=0")
<__builtin__.OracleCursor on <cx_Oracle.Connection to user username@local>>
>>> cr.description
[('DUMMY', <type 'cx_Oracle.STRING'>, 1, 1, 0, 0, 1)]
like image 116
Luke Woodward Avatar answered Nov 14 '22 21:11

Luke Woodward