2009-06-05 4 views
6

Ich arbeite an einem a-Handler für das Python-Logging-Modul. Das protokolliert im Wesentlichen zu einer Oracle-Datenbank.Spalteninformationen in cx_oracle erhalten, wenn die Tabelle leer ist?

Ich verwende cx_oracle, und etwas, das ich nicht weiß, wie man die Spaltenwerte erhält, wenn die Tabelle leer ist.

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 

Die Ausgabe lautet:

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

Jetzt im var Daten suchen i die Datentypen sehen können, und ihre Größen (Anzahl nones?), Aber das ist der Spaltenname fehlt.

Wie kann ich darüber gehen?

Antwort

13

Ich denke, das description Attribut kann das sein, was Sie suchen. Dies gibt eine Liste von Tupeln zurück, die die Spalten der zurückgegebenen Daten beschreiben. Es funktioniert ziemlich glücklich, wenn keine Zeilen zurückgegeben werden, zum Beispiel:

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