On Fri, Jun 19, 2009 at 8:16 PM, jorma kala<jjk...@gmail.com> wrote: > Hi, > Is there a way of retrieving the value of columns in the rows returned by > fetchall, by column name instead of index on the row? > Code Snippet: > > query="select * from employees" > db=MySQLdb.connect(host=host,user=user,passwd=passwd,db=database) > cursor = db.cursor () > cursor.execute (query) > rows = cursor.fetchall () > > for row in rows: > print row[0] > > > Instead of specifying the index of the row to retrieve the first column > (row[0]), I'd like to retrieve the value of the first column by column name. > Something like row.get('employee_id') > Is something of the sort possible with Mysqdb? > Thanks very much. >
Use a DictCursor: import MySQLdb.cursors . . . cursor = db.cursor (MySQLdb.cursors.DictCursor) cursor.execute (query) rows = cursor.fetchall () for row in rows: print row['employee_id'] -- kushal -- http://mail.python.org/mailman/listinfo/python-list