当表为空时,在cx_oracle中获取列信息?

时间:2009-06-05 14:13:19

标签: python cx-oracle

我正在为python日志记录模块开发一个处理程序。这基本上记录到oracle数据库。

我正在使用cx_oracle,当表为空时,我不知道如何获取的是列值。

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

输出结果为:

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]>

现在查看var数据,我可以看到数据类型及其大小(计算nones?),但那些数据类型缺失了。

我怎么能这样做?

1 个答案:

答案 0 :(得分:13)

我认为description属性可能就是你要找的东西。这将返回一个元组列表,用于描述返回数据的列。如果没有返回任何行,它会很愉快地工作,例如:

>>> 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)]