Iterating over PyoDBC result without fetchall()

pyodbc, python, sql

Solution

Sure - use a `while` loop with `fetchone`.

http://code.google.com/p/pyodbc/wiki/Cursor#fetchone

row = cursor.fetchone()
while row is not None:
    # do something
    row = cursor.fetchone()

edit In fact, doing it using the cursor directly as an iterator as shown in https://stackoverflow.com/a/59738011/2337736 is more idiomatic.

Problem

I'm trying to process a very large query with pyodbc and I need to iterate over the rows without loading them all at once with fetchall(). Is there a good and principled way to do this?

Original source