I recently ran into an annoying little snag when using MySQL. Apparently MySQL cannot handle ISO 8601 datetime values such as used by XML-RPC that use the format YYYYMMDDTHH:MM:SS. MySQL will simply convert such values to 0000-00-00 00:00:00 with a warning. I have reported this bug to MySQL but I was still faced with the unpleasant prospect of converting all my XML-RPC dateTime values to something MySQL could understand.
I searched for a way to do this automatically and I came across a way tucked inside the MySQLdb Python module. MySQLdb contains a map of conversion functions and datatypes between MySQL's column types and Python's datatypes. It is possible to extend this list and pass it to the connect() method and have any datatype supported that you want. If you don't care for the details and just want to see the code, just skip to the end.
In MySQLdb.converters.py you can find a map called conversions that simply maps Python datatypes to a converter function and MySQL column types to Python types. Type conversion happens with the use of this map whenever you qyery the database for results or whenever you insert Python variables in SQL queries using the DB API's format. If a conversion for a Python type to a MySQL type cannot be found, MySQLdb simply tries to call that object's __str__() method and tries to use that result instead.
Now you can see where my problem came in: the DateTime type used in xmlrpclib is not in MySQLdb's conversion map, and the DateTime's __str__() method returned a format of YYYYMMDDTHH:MM:SS.
To convert your own datatype automatically to something MySQL can handle, you need to define a function that will accept as arguments the object you want to convert and a dictionary. You don't need the dictionary usually but it's a required argument nonetheless. And here's the code that shows how I did this for xmlrpclib's DateTime:
- #!/usr/bin/env python
- import MySQLdb
- import MySQLdb.converters
- from xmlrpclib import DateTime
- # define the DateTime conversion
- def DateTime2string(object, dict):
- iso = object.__str__()
- sql = iso[0:4] + '-' + iso[4:6] + '-' + iso[6:]
- # properly escape the value when returning
- return MySQLdb.converters.Thing2Literal(sql, dict)
- # get the conversion table
- conv_dict = MySQLdb.converters.conversions
- conv_dict[DateTime] = DateTime2string
- # connect to the server with the new conversion table
- db = MySQLdb.connect(
- host='localhost',
- user='username',
- passwd='password',
- db='database',
- conv=conv_dict
- )
And that's all there is to it! You can convert any kind of object to a proper MySQL string representation this way. Happy coding!
Comments
#1 Anonymous Coward
#2 Anonymous Coward2
#3 Sander Marechal (http://www.jejik.com)
#4 Anonymous Coward3
#5 Sander Marechal (http://www.jejik.com)
Comments have been retired for this article.