SV: Function to convert fetchone() result from a list to a dictionary

Gerhard Häring gh at
Tue Apr 29 14:02:23 CEST 2003

Carsten Gehling wrote:
>>Fra: python-list-admin at
>>[mailto:python-list-admin at]Pa vegne af Neil Padgen
>>Sendt: 29. april 2003 11:16
>>Take a look at the cursorclass argument:
> <snip>
>>>>>c = db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
> <snip>
> Hmm... Nice. However, is this MySQLdb.cursors.DictCursor available in other
> DB modules 


> - say, if I someday changed to (shudder) Postgres or Oracle?

I don't use (shudder) MySQL :-P

pyPgSQL has it's own tuple type that will also allow you to access 
columsn via name:

for row in cursor.fetchall():
	print row.FIELD_1, row[2], row["FIELD_2"] # 3 syntaxes possible

For my Oracle uses, I adapted pyPgSQL's PgResultSet class and built a 
generic DB-API wrapper around it (it wraps connections and cursors, such 
that all cursor.fetchXX now return PgResultSets).

AFAIK both MySQLdb and psycopg support the .dictfechXXX() methods.

My favourite so far is db_row ( as 
it can be used with any database and seems to be pretty darn fast. Our 
plans are to adapt this or develop something similar for pyPgSQL 3 and 
PySQLite 0.5.

-- Gerhard

More information about the Python-list mailing list