[Tutor] Fetching dictionaries using MySQLdb

Kent Johnson kent37 at tds.net
Mon Aug 8 13:32:54 CEST 2005


Jan Eden wrote:
> Hi,
> 
> in Perl's DBI, I used fetchrow_hashref() to receive a database row as
> a dictionary, with the field names being the dictionary keys.
> 
> MySQLdb's fetchone() returns a tuple Unfortunately, I have a
> dictionary of SQL queries which return rows of different lengths
> (read: with a varying number of fields).
> 
> The documentation for MySQLdb says that fetchoneDict() is deprecated
> and the usage of fetchone() is suggested.

You could just use fetchoneDict(); deprecated isn't the same as gone...
> 
> Is there a recommended way to receive the results of an SQL query in
> the form I need? Or do I have to create a dictionary of fieldname
> tuples which can be zipped with the query result tuple?

You can create a list of field names from the cursor.description attribute. But if the number of fields in the result varies how do you know which field goes with which description?

Kent



More information about the Tutor mailing list