Получение информации о столбце в cx_oracle, когда таблица пуста?
Вопрос
Я работаю над обработчиком a для модуля ведения журнала 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, я вижу типы данных и их размеры (количество нулей?) но здесь отсутствует название столбца.
Как я могу это сделать?
Решение
Я думаю, что 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