Sunday, January 25, 2009

Python & MySQL

I have been doing some work with Python & MySQL lately and had need to use the executemany feature. This is used to batch SQL commands like for an insert.

The queries that I have were using named parameters for convenience, and I was passing a list of dictionaries to the executemany function. I expected this to work fine, as it does for other drivers (like sqlite3). However in version I got the following error.

incomplete format error on executemany with mapped variables

It appears that this is a bug in the MySQL Python driver. This is a link to the bug note.

Fortunately there is a patch that can be implemented. You just have to edit one of the Python files in the driver and update the regular expression used to parse the query for the named parameters.

One additional note. If you decide to recompile and are using Mac OS Leopard, then you will need to make a couple of additional modifications. Here are the steps that I took, based on the post here.

1.) Download the x86 TAR file of MySQL from their website. (Only if you do not have it already)
2.) Extract the TAR file somewhere. (You do not need to install MySQL if you do not want to)
3.) Download the MySQLdb Driver file and extract it.
3.) If you do not install MySQL, in the MySQLdb driver edit the file. Look for line 26, it should be like this mysql_config.path = . Where that is pointing to the locaton of the mysql_config script. Set this to the full path of that file, it should be in the bin directory of the MySQL files that you extracted.
4.) Then edit the file _mysql.c (as in the linked post). Deleting the following lines.

#ifndef uint
#define uint unsigned int

5.) Run the normal 'python build'. This should work without error. Make sure you delete the build and / or dist directories if they currently exist. This will cause a fresh build.
6.) Then run the normal 'python install'. This will install everything.
7.) At this point there is one last thing that needs to be done. Copy the file libmysqlclient_r.16.dylib from the lib directory of MySQL to the /usr/local/mysql/lib directory. I had to do this because there was currently a different version (libmysqlclient_r.15.dylib) of the file in that directory.

Tuesday, January 13, 2009

VirtualBox 2.1 Upgrade Windows Host

I had a annoying problem while upgrading VirtualBox on my Windows Host.

One of the major improvements of version 2.1 is that Host based networking has been made significantly easier. No longer is it necessary to have TAP devices and bridge them together. Now it just works, almost...

The problem I had was that once VirtualBox was upgraded my configuration was no longer valid. My configuration files were still expecting to use the TAP devices that were previously installed.

So I went under the network configuration section of the VirtualBox GUI to update the device selection. However where the list of network devices usually is displayed there was nothing. Just an empty list.

In previous versions, you could just add a new TAP device and go from there. However that is no longer required (or an option), so the question is how do I get a device in the list?

Fortunately I'm not alone and someone has already solved this issue on the forum. Here is a link.

In case the link doesn't work.

The trick is to go into your network control panel, select the device's properties, and under the general tab in the list where it says This connection uses the following items check the item for VirtualBox Host Interface Networking Driver.

Monday, January 05, 2009

Python Simple Database Library

I have just released the second version of pysimpledb. It is a simple wrapper to the standard Python DB-API 2.0 which aims to take the repetitive boiler plate code out of creating a good database access layer.

The project is not a full ORM and is not meant to be. It is fashioned after projects like Apache's iBatis and Spring's JDBC templates, and is intended for use in cases where it is important to be able to directly control the SQL that is being used.

Here is a link to the project page, link.

Please try and and let me know what you think.