[Tutor] Cursors as Dictionaries

Danny Ruttle danny@intuitivemedia.com
Wed, 11 Apr 2001 10:13:46 +0100


I understand that the Python DB2 API allows the
results of a database query to be returned as a
dictionary.

I have tried the following:

 >>> import PgSQL
 >>> import libpq
 >>>
 >>> db_init = PgSQL.connect(database="testdb3")
 >>> my_cursor = db_init.cursor()
 >>> my_cursor.execute("select user_id, first_name from ruser")
 >>> dict = {}
 >>> dict = my_cursor.fetchall()

but when I try to access the dictionary I get the following error:

 >>> h = dict.keys()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: keys

On further investigation I discovered that the type of
dict is now a list!!

 >>> dir(dict)
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 
'reverse', 'sort']

 >>> print dict[0]
['abf1tu', 'Amy']

This isn't what I want - I need the field names as well as the data in the
result set.

regards
Danny