comparing results of fetchall() with a known value

Skip Montanaro skip at
Tue Aug 21 02:13:52 CEST 2001

    Jeff> Here's what I've done: I created an odbc connect to the database,
    Jeff> opened a cursor, executed the sql, then set a variable: namelist =
    Jeff> cursor.fetchall().  What's got me stumped is that "namelist" is a
    Jeff> list of tuples with sample data like: [('smith',), ('johnson',),
    Jeff> ('jackson',), ('jones',)]

That's what it's supposed to do.  If you execute something like

    select a,b,c from mytable where name="monty python"

You'd get a list of three-element tuples from fetchall(), one for each row
in the database that matched the where clause.  For consistency, if you

    select a from mytable where name="monty python"

It still returns a list of tuples, it's just that each tuple only has one
element.  This allows you to treat the results of all queries in a similar

One thing I'm about to experiment with is MySQLdb's DictCursor.  Instead of
returning a list of tuples it returns a list of dicts, whose keys are the
field names, e.g.:

    [{"a": "manny", "b": "moe", "c": "jeff"}, ...]


    [{"mytable.a": "manny", "mytable.b": "moe", "mytable.c": "jeff"}, ...]

The problem with returning lists of tuples is that your output order is
somewhat fragile.  With dicts, you don't have that problem.

Skip Montanaro (skip at

More information about the Python-list mailing list