Python script to print out table data in name/value pairs

Simple today. I just wanted to ensure I had this in case I need to cut and paste it later 🙂

#!/home/oracle/local/bin/python

import cx_Oracle, string

connection = cx_Oracle.connect(mode = cx_Oracle.SYSDBA)

cursor = connection.cursor()
cnt=cursor.execute("select * from v$database")

colcount=len(cnt.description)

for row in cursor.fetchall():
  print "-----------------------------------------------"
  for col in range(colcount):
    print string.ljust(cursor.description[col][0],40) + str(row[col])
  print "-----------------------------------------------"

cursor.close()
connection.close()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.