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

Hi, I am currently using the below technique to return a list of dicts instead of a tuple of tuples from adbapi. Is there a cleaner way? def dictQuery(self, query): self.execute(query) result = self.fetchall() columns = [d[0] for d in self._cursor.description] return [dict(zip(columns, r)) for r in result] dbPool = adbapi.ConnectionPool('MySQLdb', db, user, pass, host) dbPool.runInteraction(dictQuery, "SELECT whatever FROM table" ).addCallback(callBackFun) Cheers Einar

On Mon, 2007-02-19 at 16:39 +0100, "Einar S. Idsø" wrote:
Some database adapters will return objects that act like dictionaries, or have an extra method that does so. adabpi itself doesn't really provide any extra infrastructure for doing so since it's intended as a thin layer wrapping db-api.

Einar S. Idsø wrote:
yes
using one of the other adapters? If so, would you mind pointing me to
only if it provides dictfetchall() support
some of my options as I haven't been able to find any simple alternatives to adbapi?
You misunderstand - you have to use adbapi. The choice is which python DB-API adapter the adbapi connection will "wrap". I'm afraid I don't use MySQL so I don't know if any have dict* support.

On Tue, 20 Feb 2007 05:40:17 -0600, Phil Mayers <p.mayers@imperial.ac.uk> wrote:
The MySQLdb adapter has a DictCursor, which subclasses the usual dbapi cursor, and should meet the original poster's needs. Another solution is to use something like db_row (Google for it). Hope this helps, L. Daniel Burr

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? 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 :) Cheers, Einar

On Wed, 21 Feb 2007 18:12:45 -0600, Einar S. Idsø <einar.twisted@norsk-esport.no> wrote:
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( MySQLdb, host="my.host.com", port=1234, cursorclass=MySQLdb.cursors.DictCursor ) d = pool.runQuery('SELECT * FROM SomeTable') d.addErrback(log.err) d.addCallback(onResultSet)
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

On Mon, 2007-02-19 at 16:39 +0100, "Einar S. Idsø" wrote:
Some database adapters will return objects that act like dictionaries, or have an extra method that does so. adabpi itself doesn't really provide any extra infrastructure for doing so since it's intended as a thin layer wrapping db-api.

Einar S. Idsø wrote:
yes
using one of the other adapters? If so, would you mind pointing me to
only if it provides dictfetchall() support
some of my options as I haven't been able to find any simple alternatives to adbapi?
You misunderstand - you have to use adbapi. The choice is which python DB-API adapter the adbapi connection will "wrap". I'm afraid I don't use MySQL so I don't know if any have dict* support.

On Tue, 20 Feb 2007 05:40:17 -0600, Phil Mayers <p.mayers@imperial.ac.uk> wrote:
The MySQLdb adapter has a DictCursor, which subclasses the usual dbapi cursor, and should meet the original poster's needs. Another solution is to use something like db_row (Google for it). Hope this helps, L. Daniel Burr

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? 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 :) Cheers, Einar

On Wed, 21 Feb 2007 18:12:45 -0600, Einar S. Idsø <einar.twisted@norsk-esport.no> wrote:
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( MySQLdb, host="my.host.com", port=1234, cursorclass=MySQLdb.cursors.DictCursor ) d = pool.runQuery('SELECT * FROM SomeTable') d.addErrback(log.err) d.addCallback(onResultSet)
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

L. Daniel Burr wrote:
Wow, thank you! Now /there/ is the proper way of returning a dict :) I just can't believe it was that easy...
It was just the help I needed: An example to solve my problem, and a much-needed explanation of adbapi. Perfect! Thank you so much, Einar
participants (4)
-
"Einar S. Idsø"
-
Itamar Shtull-Trauring
-
L. Daniel Burr
-
Phil Mayers