Odbc.py

From PeformIQ Upgrade
Jump to navigation Jump to search
#!/usr/bin/env python

import dbi, odbc     # ODBC modules
import time          # standard time module

dbc = odbc.odbc(     # open a database connection
    'sample/monty/spam'  # 'datasource/user/password'
    )
crsr = dbc.cursor()  # create a cursor
crsr.execute(        # execute some SQL
    """
    SELECT country_id, name, insert_change_date
    FROM country
    ORDER BY name
    """
    )
print 'Column descriptions:'  # show column descriptions
for col in crsr.description:
    print ' ', col
result = crsr.fetchall()      # fetch the results all at once
print '\nFirst result row:\n ', result[0]  # show first result row
print '\nDate conversions:'   # play with dbiDate object
date = result[0][-1]
fmt = '  %-25s%-20s'
print fmt % ('standard string:', str(date))
print fmt % ('seconds since epoch:', float(date))
timeTuple = time.localtime(date)
print fmt % ('time tuple:', timeTuple)
print fmt % ('user defined:', time.strftime('%d %B %Y', timeTuple))