[DB-SIG] Newbie trying to fetch individual rows w/ PostgreSQL

Andy Dustman andy@dustman.net
Mon, 4 Jun 2001 02:44:20 -0400 (EDT)

On Fri, 1 Jun 2001, Kevin Cole wrote:

> Hi,
> I've got a little test code that sorta works, but not as well as I'd
> like.  fetchall() does what I want, fetchone() appears to fetch the same
> row cursor.rowcount times, rather than fetching the next row.  Here's
> what I'm doing:
>     import pgdb
>     mydb = pgdb.connect("localhost:mydb")
>     curse = mydb.cursor()
>     curse.execute("select * from mytable where state = 'MD')
>     for hits in range(curse.rowcount):
>        print curse.fetchone()
> If I use "for hits in curse.fetchall():" I get what I expect, and
> printing curse.rowcount yields the correct number of rows.  What am I
> misunderstanding?

Nothing that I can see. Each fetchone() invocation should return a new
row, so this sounds like a pgdb bug. I wouldn't use range(curse.rowcount)
myself; more likely I'd use fetchall(). If the result set can be
arbitrarily large, I'd do something like this:

row = curse.fetchone()
while row:
    print row
    row = curse.fetchone()

Andy Dustman         PGP: 0xC72F3F1D
    @       .net     http://dustman.net/andy
"I'd rather listen to Newton than to Mundie. He may have been dead for almost
three hundred years, but despite that he stinks up the room less." -- Linus T.