[Twisted-Python] How to properly return a dict from adbapi

L. Daniel Burr ldanielburr at mac.com
Sat Feb 24 12:48:46 EST 2007

On Wed, 21 Feb 2007 18:12:45 -0600, Einar S. Idsø  
<einar.twisted at norsk-esport.no> wrote:

> Phil: Thank you for your answer. I think I understand now, but the first
>  question below will show if I'm right or way off:
> L. Daniel Burr wrote:
>> The MySQLdb adapter has a DictCursor, which subclasses the usual dbapi
>> cursor, and should meet the original poster's needs.
> I suppose that when I use adbapi.ConnectionPool('MySQLdb',...), I am
> already using the MySQLdb adapter? It is straightforward to instruct
> MySQLdb to use the DictCursor when programming in straight (i.e.
> non-Twisted) environments, but how do I do the same from Twisted, using
> t.e.a.ConnectionPool?

This is very easy to do, because adbapi.ConnectionPool will pass on
connection-specific parameters to the underlying DBAPI module.

Example (untested):

import MySQLdb
import twisted.enterprise.adbapi as db
import twisted.python.log as log

def onResultSet(resultSet):
     for row in resultSet:
         # Do stuff

pool = db.ConnectionPool(

d = pool.runQuery('SELECT * FROM SomeTable')

> Please let a novice Twisted-programmer know if I am completely missing
> the mark here, and I will go back to my cave with my original solution.
> I'm just trying to understand the intricacies of the Twisted APIs and
> lingo/semantics, and doing things right the second time :)

The key thing to understand in this case is that adbapi is just a simple
wrapper around a given DBAPI module, so if you know how to configure
MySQLdb, then configuring adbapi.ConnectionPool is very similar.  Just
pass the same keyword args to ConnectionPool, that you would pass to
MySQLdb, and you're good to go.

> Cheers,
> Einar

Hope this helps,

L. Daniel Burr

More information about the Twisted-Python mailing list