MySQLdb - Tuples

Georg Brandl g.brandl-2005-01 at gmx.net
Wed Feb 2 11:39:55 EST 2005


Andy Dustman wrote:
> #########################################33
> #! /usr/bin/env python
> import MySQLdb
> db=MySQLdb.connect(host='localhost', db='photum_0_6_2', user='root',
> passwd='thkhgfgd')
> c=db.cursor()
> c.execute('select person from persons order by person')
> for (person,) in c: # or c.fetchall() (may be more portable)
> print person
> ################################################
> 
> If you return more than one column, then you don't need the parentheses
> for the tuple unpacking.

You don't need the parentheses for one element either:

for person, in c:
    print person

works perfectly. Note the trailing comma.

Georg



More information about the Python-list mailing list