テーブルが空の場合cx_oracleの列情報を取得しますか?
質問
私は、Pythonのloggingモジュール用のハンドラに取り組んでいます。これは、基本的に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のデータを見て、そしてそれらのサイズ(nonesを数える?)が、列名がありませんthatsの。
どのように私はこれについて行くことができますか?
解決
私は 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)]
所属していません StackOverflow