[英]How to do while() the “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 of doing this? 这种“pythonic”方式是什么?
You don't have to use while
loop at all, because cursors are iterable: 您根本不必使用
while
循环,因为游标是可迭代的:
for row in cursor:
print(row)
From the "Connections and cursors" section of Django documentation : 从Django文档的“连接和游标”部分:
connection and cursor mostly implement the standard Python DB-API described in PEP 249 — except when it comes to transaction handling.
连接和游标主要实现PEP 249中描述的标准Python DB-API - 除了涉及事务处理。
From the mentioned PEP 249 : 从提到的PEP 249 :
Cursor.next()
Cursor.next()
Return the next row from the currently executing SQL statement using the same semantics as .fetchone()
使用与.fetchone()相同的语义从当前执行的SQL语句返回下一行
Cursor.__iter__()
光标.__ ITER __()
Return self to make cursors compatible to the iteration protocol
返回self以使游标与迭代协议兼容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.