import dbi, odbc # ODBC modulesimport time # standard time module
dbc = odbc.odbc( # open a database connection 'sample/monty/spam' # 'datasource/user/password' )crsr = dbc.cursor() # create a cursorcrsr.execute( # execute some SQL """ SELECT country_id, name, insert_change_date FROM country ORDER BY name """ )print 'Column descriptions:' # show column descriptionsfor col in crsr.description: print ' ', colresult = crsr.fetchall() # fetch the results all at onceprint 'First result row: ', result[0] # show first result rowprint 'Date conversions:' # play with dbiDate objectdate = 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))-------------------------------output--------------------------------
Column descriptions: ('country_id', 'NUMBER', 12, 10, 10, 0, 0) ('name', 'STRING', 45, 45, 0, 0, 0) ('insert_change_date', 'DATE', 19, 19, 0, 0, 1)
First result row: (24L, 'ARGENTINA', )
Date conversions: standard string: Fri Dec 19 01:51:53 1997 seconds since epoch: 882517913.0 time tuple: (1997, 12, 19, 1, 51, 53, 4, 353, 0) user defined: 19 December 1997