在表为空时如何在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]>
现在看着变量data,我可以看到数据类型和它们的大小(计算一下空值?),但是缺少了列名。
我该怎么做呢?
1 个回答
14
我觉得你可能在找的是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)]