-1

I want to be able to do something like the code below (which is PHP):

while($row = mysql_fetch_array($result)){
    $name = $row['name'];
    $event = $row['event'];

in python. So getting an element by its index. What is the best way?

trainoasis
  • 6,419
  • 12
  • 51
  • 82

2 Answers2

3

Yes, using dictionaries, which are built in types.

d = {'name': 'user'}
print d['name']
user
jro
  • 9,300
  • 2
  • 32
  • 37
1

You use the DictCursor object from the MySQLdb package. This will produce a dictionary (mapping) in the result set rather than the usual tuples.

import MySQLdb                                                                                                                      
from MySQLdb import cursors

def fetchallmap(self, q, *args):                                                                                                
    """Fetch all rows into a dictionary.                                                                                        
    """                                                                                                                         
    curs = self._connection.cursor(cursors.DictCursor)                                                                                   
    curs.execute(q, args)                                                                                                       
    res = curs.fetchall()                                                                                                       
    curs.close()                                                                                                                
    self._connection.commit()                                                                                                            
    return res

This will return a list of dictionaries (mappings). If you just want one row, just substitute fetchoone instead of fetchall.

def fetchonemap(self, q, *args):                                                                                                
    """Fetch one row into a dictionary.                                                                                         
    """                                                                                                                         
    curs = self._connection.cursor(cursors.DictCursor)                                                                                   
    curs.execute(q, args)                                                                                                       
    res = curs.fetchone()                                                                                                       
    curs.close()                                                                                                                
    self._connection.commit()                                                                                                            
    return res                                                                                                                  
Keith
  • 42,110
  • 11
  • 57
  • 76
  • could you show me how this would work in practice for my case please? How would i get 'name' and other variables for each row given? – trainoasis Oct 31 '12 at 13:08
  • 1
    here is an example : [http://www.kitebird.com/articles/pydbapi.html#TOC_10](http://www.kitebird.com/articles/pydbapi.html#TOC_10) – lucasg Oct 31 '12 at 13:21
  • post as 'answer' so i can accept it :)) that worked! thanks alot – trainoasis Oct 31 '12 at 13:50