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

Alex Martelli aleax at aleax.it
Wed Jan 16 10:47:32 EST 2002


"Gabriel Ambuehl" <gabriel_ambuehl at buz.ch> wrote in message
news:mailman.1011193358.24672.python-list at python.org...
    ...
> 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.
>
> So something like:
>
> db.execute("myquery")
> db.fetchallDict()
>
> db.execute("mybigquery")
> db.fetchallTuple()

Wrap the cursor object (by inheritance if feasible, otherwise by automatic
delegation, http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295 )
and have your fetchallTuple just call fetchall, while fetchallDict
builds the dictionary on top of that (using cursor.description too).


Alex






More information about the Python-list mailing list