[DB-SIG] cursor.fetchiter()?

M.-A. Lemburg mal at egenix.com
Wed Jun 22 15:31:38 CEST 2005


Anthony Baxter wrote:
> Would it make sense for DB-SIG compliant modules to support a 
> cursor.fetchiter() type method, to use the iterator protocol
> (instead of fetchall()'s current load-everything-into-memory
> first approach). Clever extensions could be written to grab 
> N at a time, where N is less than 10,000,000, but more than 1 -
> this then means you get faster performance than fetchone(),
> and a less sucky API.
> 
> res = cursor.fetchone()
> while res:
>     dostuffwith(res)
>     res = cursor.fetchone()
> 
> vs
> 
> for res in cursor.fetchiter():
>     dostuffwith(res)
> 
> And yes, you can get something that's most of the way there 
> now with something like the code in the cookbook entry
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/137270 - 
> just wondering if it's something worth adding to the API. 

>From the DB API (in the standard extensions section):

    Cursor Method .next()

        Return the next row from the currently executing SQL statement
        using the same semantics as .fetchone().  A StopIteration
        exception is raised when the result set is exhausted for Python
        versions 2.2 and later. Previous versions don't have the
        StopIteration exception and so the method should raise an
        IndexError instead.

        Warning Message: "DB-API extension cursor.next() used"

    Cursor Method .__iter__()

        Return self to make cursors compatible to the iteration protocol.

        Warning Message: "DB-API extension cursor.__iter__() used"


The only difference compared to your proposal is that you'd
write:

for res in cursor:
    dostuff(res)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jun 22 2005)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the DB-SIG mailing list