python 使用 .fetchmany() 循环遍历 sqlite select 直到没有剩余条目

Sca*_*rix 1 python sqlite

我需要一次从 sqlite3 数据库中检索 160 行结果,并重复该操作,直到没有任何行可供查询,这就是我所拥有的:

conn = sqlite3.connect("C:\\Users\\%s\\AppData\\Roaming\\GridcoinResearch\\reports\\Rain.db" % user_account)
c = conn.cursor()
conn.text_factory = str
address = c.execute('select Address from NNDATA where NeuralMagnitude != 0 and NeuralMagnitude is not null and CPID in (select cpids from GRIDCOINTEAM)').fetchmany(160)
conn.text_factory = float
nn_mag = c.execute('select NeuralMagnitude from NNDATA where NeuralMagnitude != 0 and NeuralMagnitude is not null and CPID in (select cpids from GRIDCOINTEAM)').fetchmany(160)
conn.close()

while True:
    if nn_mag == ():
        sys.exit("Complete")
Run Code Online (Sandbox Code Playgroud)

原因是我在和 之间sys.exit还有一堆其他代码,所以当最后一个循环完成时我可以退出程序。现在它正在执行第一遍,然后 cmd.exe 挂起。conn.close()while True:

编辑:刚刚发布,我不告诉循环选择 NEXT 160,天哪!

Kas*_*mvd 6

fetchmany如果没有剩余项目,该属性将返回一个空列表,因此您只需检查其结果的验证即可。另请注意,您应该从查询中删除limitfetchall. 因为使用的全部本质fetchmany是从游标对象中获取有限的结果。

chunk_size = 160
while True:
    result = nn_mag.fetchmany(chunk_size)
    if not result:
        sys.exit("Complete")
    else:
         # do something with result
Run Code Online (Sandbox Code Playgroud)