How to do while() the "pythonic way"
Question: I want to do this:

    
    
    from django.db import connection
    
    cursor = connection.cursor()
    cursor.execute("PRAGMA table_info(ventegroupee)")
    while row = cursor.fetchone():
        print(row)
    

I get this:

    
    
      File "<input>", line 1
        while row = cursor.fetchone():
                  ^
    SyntaxError: invalid syntax
    

What is the "pythonic" way of doing this?


Answer: You don't have to use `while` loop at all, because cursors are iterable:

    
    
    for row in cursor:
        print(row)
    

From the "Connections and cursors" section of [Django
documentation](https://docs.djangoproject.com/en/1.8/topics/db/sql/#connections-
and-cursors):

> _connection_ and _cursor_ mostly implement the standard Python DB-API
> described in PEP 249 — except when it comes to transaction handling.

From the mentioned [PEP 249](https://www.python.org/dev/peps/pep-0249/#next):

> _Cursor.next()_
>
> Return the next row from the currently executing SQL statement using the
> same semantics as _.fetchone()_
>
> _Cursor.__iter__()_
>
> Return self to make cursors compatible to the iteration protocol

