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

Ube*_*per 6 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
Run Code Online (Sandbox Code Playgroud)

输出是:

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]>
Run Code Online (Sandbox Code Playgroud)

现在看一下var数据,我可以看到数据类型及其大小(计算nones?),但是缺少列名.

我怎么能这样做?

Luk*_*ard 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)]