How to do while () "pythonic way"

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 to do this?

+4
source share
2 answers

You don’t have to use a loop whileat all because cursors are iterable:

for row in cursor:
    print(row)

In the "Links and Cursors" section of the Django Documentation :

the connection and cursor basically implement the standard Python DB-API described in PEP 249, except when it comes to transaction processing.

From the mentioned PEP 249 :

Cursor.next ()

Returns the next line from the currently executing SQL statement using the same semantics as .fetchone ()

Cursor .__ ITER __ ()

self,

+11

for, :

for row in cursor:
  ...

python:

+3

All Articles