我想这样做:
from django.db import connection
cursor = connection.cursor()
cursor.execute("PRAGMA table_info(ventegroupee)")
while row = cursor.fetchone():
print(row)
我明白了:
File "<input>", line 1
while row = cursor.fetchone():
^
SyntaxError: invalid syntax
这样做的“pythonic”方式是什么?
最佳答案
您根本不必使用 while
循环,因为游标是可迭代的:
for row in cursor:
print(row)
来自 Django documentation 的“连接和游标”部分:
connection and cursor mostly implement the standard Python DB-API described in PEP 249 — except when it comes to transaction handling.
来自提到的PEP 249 :
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
关于python - 如何做 while() "pythonic way",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32199150/