[英]In python's sqlite3 module, will using cursor.fetchone() as the condition for a while loop fetch a row?
我正在使用python的sqlite3模块。
我想遍历表中的行,直到没有剩下的行。 我想一次做一个而不是使用fetchall
因为表非常大。 我在python sqlite3文档中curs.fetchone()
当光标选择中没有任何行时, curs.fetchone()
返回None
。
所以我有想法在一个while fetchone():
循环中运行我的代码,在循环中发生'实际' fetchone()
调用,如下所示:
while c.fetchone():
row = c.fetchone()
print(foo(row))
我不知道这个循环是否实际上是在while语句中获取一次,而是在body中获取一次,或者它是否仅在正文中。 哪一个?
它会执行两次fetchone
,所以这不是一个好主意。 由于sqlite3.Cursor
是可迭代的,你可以这样做:
for row in c:
print(foo(row))
或者,您可以使用无限循环并使用fetchmany
批量获取数据:
# Lets take 1000 rows at the time
batch_size = 1000
while True:
rows = c.fetchmany(batch_size)
if not rows: break
for row in rows:
print(foo(row))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.