Roundtrip SQL data especially datetime

John Nagle nagle at animats.com
Sat Dec 16 01:10:22 EST 2006


dyork wrote:
> "John Machin" <sjmachin at lexicon.net> wrote in message 
> news:1166211949.065578.292600 at f1g2000cwa.googlegroups.com...
> I was looking for a constructor that was the complement of str(). Most/many 
> languages would provide that. Sometimes it's called parse().
> 
> 
>>The constructor is datetime.datetime(year, ....., second) so the
>>following (which works all the way back to Python 2.3) seems not too
>>obscure to me:

> 
>>If you have, as you should, Python 2.5, you can use this:

    Actually, MySQLdb isn't released for Python 2.5 yet, so for
anything with a database, you need an older version of Python.

    If you really want to change the conversions for TIMESTAMP, add the
"conv" argument to "connect".  Make a copy of "MySQLdb.converters.conversions",
then replace the key "MySQLdb.FIELD_TYPE.TIMESTAMP", which normally has
the value 'mysql_timestamp_converter' with your own converter.  You can
then get the interface to emit a "datetime" object.

    Routinely converting MySQL DATETIME objects to Python "datetime"
objects isn't really appropriate, because the MySQL objects have a
year range from 1000 to 9999, while Python only has the UNIX range
of 1970 to 2038.  Remember, a database may have DATETIME dates which
reflect events in the distant past or future.

    None of this will help performance; dates and times are sent over the
connection to a MySQL database as strings.

				John Nagle



More information about the Python-list mailing list