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 データを見ると、データ型とそのサイズ (count nones?) を確認できますが、列名がありません。

これについてどうすればいいですか?

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.description
[('DUMMY', <type 'cx_Oracle.STRING'>, 1, 1, 0, 0, 1)]
于 2009-06-05T19:11:16.857 に答える