mysql-python with cursor that returns either dicts or tuples?

Cliff Wells logiplexsoftware at earthlink.net
Wed Jan 16 14:20:13 EST 2002


On Wed, 16 Jan 2002 16:03:43 +0100
Gabriel Ambuehl wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> 
> Hello,
> I'm wondering whether there is any solution to have mysql-python use
> a
> Cursor class that can return either a dictionary or a tuple since I'd
> like to use one single cursor instance in my app but in some places a
> tuple is better (for performance reasons) whereas in others, a
> dictionary is what's needed.

It's been a while since I've done anything using MySQL with Python, but
looking at the docs for 
MySQL-python-0.9.0 I see that it is possible to create your own cursor
classes:

"""

Because MySQLdb's Connection and Cursor objects are written in Python, you
can easily derive your own subclasses. There are several Cursor classes in
MySQLdb.cursors: 

[snip a lot of other classes]

CursorDictRowsMixIn
Causes the cursor to return rows as a dictionary, where the keys are column
names and the values are column values. Note that if the column names are
not unique, i.e., you are selecting from two tables that share column
names, some of them will be rewritten as table.column. This can be avoided
by using the SQL AS keyword. (This is yet-another reason not to use * in
SQL queries, particularly where JOIN is involved. 

"""

-- 
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308




More information about the Python-list mailing list