[mod_python] mod_python + MySQLdb + Apache strange behavoir

Jorey Bump list at joreybump.com
Thu Sep 15 09:01:15 EDT 2005

Earle Ady wrote:

> As for the MySQLdb inconsistencies, varying error messages, etc.  The  
> problem was actually quite simple!  I was using  
> MySQLdb.cursors.DictCursor as a cursor type and only importing MySQLdb.
> I added a simple import for MySQLdb.cursors, and all of my problems  
> went away.   This is something I never would have guessed from the  
> behavioir and errors of mod_python and MySQLdb linkage, but a lot of  
> persistence and a truckload of tenacity managed to dig it up.
> It's bizarre that it would work at all, given the fix.   If anyone  else 
> is having these problems, double check to see what type of  cursor they 
> are using in MySQLdb, and if they are doing the import  specifically.  
> It should have been more clear as using the standard  cursor never had a 
> problem but I obviously overlooked this for awhile.

This extra import step is unnecessary. The following works fine:

import MySQLdb

_dbh = MySQLdb.connect(host = "localhost", user = "me", passwd = "foo", 
db = "bar")

def get_users():
     # this cursor type will return a tuple of dictionaries
     cursor = _dbh.cursor(MySQLdb.cursors.DictCursor)
     query = "SELECT * FROM users"
     rows = cursor.fetchall()
     return rows

More information about the Mod_python mailing list