使用cx_Oracle發現一個示例,此示例顯示Cursor.description
的所有信息。使用cx_Oracle時更好的打印列名稱的方法
import cx_Oracle
from pprint import pprint
connection = cx_Oracle.Connection("%s/%[email protected]%s" % (dbuser, dbpasswd, oracle_sid))
cursor = cx_Oracle.Cursor(connection)
sql = "SELECT * FROM your_table"
cursor.execute(sql)
data = cursor.fetchall()
print "(name, type_code, display_size, internal_size, precision, scale, null_ok)"
pprint(cursor.description)
pprint(data)
cursor.close()
connection.close()
我想看到什麼是的Cursor.description[0]
(名稱)的列表,所以我改變了代碼:
import cx_Oracle
import pprint
connection = cx_Oracle.Connection("%s/%[email protected]%s" % (dbuser, dbpasswd, oracle_sid))
cursor = cx_Oracle.Cursor(connection)
sql = "SELECT * FROM your_table"
cursor.execute(sql)
data = cursor.fetchall()
col_names = []
for i in range(0, len(cursor.description)):
col_names.append(cursor.description[i][0])
pp = pprint.PrettyPrinter(width=1024)
pp.pprint(col_names)
pp.pprint(data)
cursor.close()
connection.close()
我認爲會有更好的方法來打印出列的名稱。請讓我選擇Python初學者。 :-)
對不起,你自己釘了;)Thanx – ant 2017-09-13 06:17:43