我需要一次从我的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")
sys.exit
的原因是我在conn.close()
和while True:
之间有许多其他代码,所以当最后一个循环完成后,我可以退出程序。现在它正在进行第一次传递,然后cmd.exe挂起。
编辑:刚刚重新启动我不告诉循环选择NEXT 160,哦亲爱的!
答案 0 :(得分:1)
如果没有项目左侧,fetchmany
属性将返回一个空列表,因此您只需检查其结果的验证。另请注意,您应从查询和limit
中删除fetchall
。因为使用fetchmany
的全部本质是从游标对象获取有限的结果。
chunk_size = 160
while True:
result = nn_mag.fetchmany(chunk_size)
if not result:
sys.exit("Complete")
else:
# do something with result
答案 1 :(得分:0)
好的答案是:
conn = sqlite3.connect("C:\\Users\\%s\\AppData\\Roaming\\GridcoinResearch\\reports\\Rain.db" % user_account)
c = conn.cursor()
position = 00
while True:
conn.text_factory = str
address_db = c.execute('select Address from NNDATA where NeuralMagnitude != 0 and NeuralMagnitude is not null and CPID in (select cpids from GRIDCOINTEAM) limit {}, 160'.format(position)).fetchall()
conn.text_factory = float
rac_db = c.execute('select rac from GRIDCOINTEAM where rac != 0 and rac is not null limit {}, 160'.format(position)).fetchall()
if not address_db:
conn.close()
sys.exit("Complete")
else:
position += 160