6

我正在为 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 数据,我可以看到数据类型及其大小(计数无?),但缺少列名。

我该怎么办?

4

1 回答 1

14

我认为该description属性可能是您正在寻找的。这将返回一个描述返回数据列的元组列表。如果没有返回任何行,它会非常愉快地工作,例如:

>>> 导入 cx_Oracle
>>> c = cx_Oracle.connect("用户名", "密码")
>>> cr = c.cursor()
>>> cr.execute("select * from dual where 1=0")
<__builtin__.OracleCursor on <cx_Oracle.Connection to user username@local>>
>>> cr.描述
[('DUMMY', <type 'cx_Oracle.STRING'>, 1, 1, 0, 0, 1)]
于 2009-06-05T19:11:16.857 回答