def iterate_cursor(cursor): l = cursor.fetchone() if l: yield l #elsewhere: cursor.execute("SELECT Blah...") for line in iterate_cursor(cursor): # do whatever