logo       

Bug report with dictcursor: msg#00005

python.db.psycopg.devel

Subject: Bug report with dictcursor


Problem :
Using the cursor as an iterable seems to yield DictRow's which behave like non-Dict rows (ie. no string keys). Bug ?

Example :

cursor = db.cursor( cursor_factory = psycopg2.extras.DictCursor )

# This code works :

cursor.execute( "SELECT id FROM users LIMIT 2" )
for row in cursor.fetchall():
print "row :", row, type(row)
for key in 0,'id':
try:
print key, row[key]
except:
traceback.print_exc()
print

# Results :

row : [1] <class 'psycopg2.extras.DictRow'>
0 1
id 1

row : [2] <class 'psycopg2.extras.DictRow'>
0 2
id 2


# Here we have removed .fetchall(), because the cursor is an iterable.

cursor.execute( "SELECT id FROM users LIMIT 2" )
for row in cursor:
print "row :", row, type(row)
for key in 0,'id':
try:
print key, row[key]
except:
traceback.print_exc()
print

# Results :

0 1
Traceback (most recent call last):
File "test.py", line 25, in ?
print key, row[key]
File "/usr/lib64/python2.4/site-packages/psycopg2/extras.py", line 75, in __getitem__
x = self._index[x]
KeyError: 'id'
id
row : [2] <class 'psycopg2.extras.DictRow'>
0 2
Traceback (most recent call last):
File "test.py", line 25, in ?
print key, row[key]
File "/usr/lib64/python2.4/site-packages/psycopg2/extras.py", line 75, in __getitem__
x = self._index[x]
KeyError: 'id'
id

#######################


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise