Erste Spalte Info in cx_Oracle wenn Tabelle leer ist?
Frage
Ich arbeite an einem Handler für das Python-Protokollierungsmodul. Das meldet im Wesentlichen zu einer Oracle-Datenbank.
Ich bin mit cx_Oracle, und etwas, das ich weiß nicht, wie die Spaltenwerte zu erhalten, 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 fehlt den Spaltennamen.
Wie kann ich über diese gehen?
Lösung
Ich denke, das description
Attribut sein kann Wonach suchen Sie. Dies gibt eine Liste von Tupeln, die die Spalten der zurückgegebenen Daten beschreiben. Es funktioniert ganz glücklich, wenn es keine Zeilen zurückgegeben, zum Beispiel sind:
>>> 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)]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow