Better ways to print out column names when using cx_Oracle
Posted
by philipjkim
on Stack Overflow
See other posts from Stack Overflow
or by philipjkim
Published on 2010-06-07T09:01:21Z
Indexed on
2010/06/08
5:02 UTC
Read the original article
Hit count: 231
Found an example using cx_Oracle, this example shows all the information of Cursor.description
.
import cx_Oracle
from pprint import pprint
connection = cx_Oracle.Connection("%s/%s@%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()
What I wanted to see was the list of Cursor.description[0]
(name), so I changed the code:
import cx_Oracle
import pprint
connection = cx_Oracle.Connection("%s/%s@%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()
I think there will be better ways to print out the names of columns. Please get me alternatives to the Python beginner. :-)
© Stack Overflow or respective owner