variable assignment in "while" loop

Andy Todd andy47 at
Tue Jul 29 18:04:15 CEST 2003

sismex01 at wrote:

>>From: Andy Todd [mailto:andy47 at] 
>>Sent: Martes, 29 de Julio de 2003 10:23 a.m.
>>Spot on, with one (minor) correction. With a for loop you have to 
>>iterate over the results of a call to a method on the cursor, e.g.;
>>for info in mydbcursor.fetchall():
>>     print "Information:", info
>>You could replace fetchall() with fetchmany() or, if you are feeling 
>>contrary, fetchone() ;-)
> Are you sure about this?  Because, if you have to iterate
> through .fetchall()  (for example), then the cursor isn't
> actually an iterable object, the result of fetchall() is
> (it's a list, which is iterable), same goes for fetchmany();
> BUT, if you iterate over the results of fetchone() then
> you're gonna do a columnwise iteration over a single row.
> :-)
> So... anyone... do cursors have an __iter__() method?
> I don't have a DB module at hand to check it out...
> -gustavo

Technically, cursor objects are not iterators, and the DB-API 
specification has this to say about the various methods that return data;


Fetch the next row of a query result set, returning a
single sequence, or None when no more data is



Fetch the next set of rows of a query result, returning a
sequence of sequences (e.g. a list of tuples). An empty
sequence is returned when no more rows are available.



Fetch all (remaining) rows of a query result, returning
them as a sequence of sequences (e.g. a list of tuples).

Which means that the cursor object itself is not an iterator, and the 
methods need only return a sequence (or sequence of sequences).

But, thats not to say that the db modules can't implement a generator to 
produce the results from the 'fetch' methods if they want to. I don't 
know if any of the database modules do, but then thats up to the module 
authors and I tend not to worry about it too much, I just trust the module.

 From the desk of Andrew J Todd esq -

More information about the Python-list mailing list