Python 2.5 and Generic Database Connection Utilities

Python 2.5 has been released today. Yay. I look forward to utilising some of its fine language features in my future code.

But, and you knew there was a gripe coming didn't you? With the inclusion of the pysqlite module as part of the standard distribution there is a problem. It has been renamed from pysqlite2 to sqlite3. A move I agree with by the way, as it is clear from the name exactly what the module supports.

The problem is I've got quite a bit of code that uses the old name. Where my existing scripts say;

>>> from pysqlite2 import dbapi2 as sqlite

They now need to say;

>>> try:

...     from pysqlite2 import dbapi2 as sqlite

... except ImportError: # for Python 2.5 and above

...     from sqlite3 import dbapi2 as sqlite

>>> myDb = sqlite.connect('sqlitedatabasefile.db')

Or they would, if I hadn't cunningly stolen an idea from SQLObject, which they borrowed from any number of other places, and written my own little connection function. So my scripts can carry on using;

>>> from utilities.dburi import get_connection

>>> myDb = get_connection('sqlite:/sqlitedatabasefile.db')

Fancy a look at the code? Oh, ok then, it's called and there is a text version of the file called Patches are gratefully accepted.