PyGres fetchone problem

Richard Papworth richard.papworth at bbsrc.ac.uk
Mon Feb 18 19:20:49 CET 2002


Playing around with the PyGres module I came across the following
problem when using fetchone to iterate over a cursor, an idiom that
I've seen used in many examples:

Python 1.5.2 (#1, May  1 2001, 10:59:55) [C] on osf1V4
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import pgdb
>>> connection = pgdb.connect(':aDatabase:::')
>>> cursor = connection.cursor()
>>> cursor.execute('select distinct database_id from database_table')
>>> cursor.rowcount
3
>>> while 1:   
...     aRow = cursor.fetchone()
...     if aRow == None: break
...     print aRow
... 
[5]
[5]
[5]
>>> cursor.execute('select distinct database_id from database_table')
>>> cursor.fetchall()
[[5], [6], [7]]
>>> 

The cursor.fetchone() loop should produce
[5]
[6]
[7]

However, although the cursor seems to be advancing through the result
set, only the first row of the result set is ever retrieved.

Am I doing something wrong here?

Cheers

Richard



More information about the Python-list mailing list