[Tutor] SQL Datetimes

Kent Johnson kent37 at tds.net
Tue Feb 15 11:54:13 CET 2005


Danny Yoo wrote:
> 
> On Mon, 14 Feb 2005, Bill Kranec wrote:
> 
> 
>>I'm using Kinterbasdb to access a Firebird database through Python, and
>>when I retrieve a row with a datetime value, I get a tuple like:
>>
>> >>> myCursor.execute( 'SELECT * FROM table' )
>> >>> for row in myCursor.fetchall():
>>        print row
>>
>>(<DateTime object for '2004-08-09 00:00:00.00' at d41720>, 'value2',
>>'value3', 'value4', 100)
>>
>>I would like to get:
>>
>>('8/9/2004', 'value2', 'value3', 'value4', 100)
> 
> 
> 
> Hi Bill,
> 
> Just out of curiosity, can you work with the datetime object itself?  It
> sounds like you want it to come out as a string, but that's somewhat
> "lossy" because we lose things like the 'time' portion of a datetime.

I'll second that suggestion...what is it you need to do with the date object?

Another alternative would be to change the database field to a string field, if that is really what 
you want. This may open it up to bad data, though.

> 
> According to the documentation here:
> 
>     http://www.python.org/doc/lib/datetime-datetime.html

Note that by default Kinterbasdb is using mxDateTime, not Python's (now) standard datetime, to 
represent the date. Docs for mxDateTime are here:
http://www.egenix.com/files/python/mxDateTime.html

There is an example on the page originally cited that shows how to use Python's datetime instead of 
mxDateTime if you prefer.
http://kinterbasdb.sourceforge.net/dist_docs/usage.html#adv_param_conv_dynamic_type_translation
scroll down until you get to the first example.

Kent



More information about the Tutor mailing list