2009-06-05 35 views
6

我正在爲python日誌記錄模塊的處理程序工作。這本質上是登錄到Oracle數據庫。當表格爲空時在cx_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數據,我可以看到的數據類型,以及它們的大小(計數諾內斯?),但多數民衆贊成缺少列名。

我該怎麼辦?

回答

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 [email protected]>> 
>>> cr.description 
[('DUMMY', <type 'cx_Oracle.STRING'>, 1, 1, 0, 0, 1)]