我有以下情况:
conn=ps.connect("dbname='xxxx' user='xxxx' password='xxxx'")
crs = conn.cursor()
statement= """ SELECT *FROM aaaa;
"""
crs.execute(statement)
# get the name of the cars
var = crs.fetchall()
但是考虑到表的大小,Ubuntu会因为内存问题而终止进程。
如何在循环中每次获取N
行?比如像这样:
N=1000
for i in range(0,10):
var = crs.fetchmany(0:N)
N+=1000
答案 0 :(得分:1)
fetchmany最多返回n个下一行,而没有剩下的则返回None。
rows = crs.fetchmany(1000)
while(rows):
# do stuff
rows = crs.fetchmany(1000)
else:
# done
答案 1 :(得分:1)
答案 2 :(得分:0)
我通过以下方式解决了这个问题:
query = """SELECT COUNT(*) FROM xxxxx;
"""
crs.execute(query)
N = crs.fetchall()
N = N[0][0]
for i in range(0,N):
if n1 == N: continue
n1 = n + 1000
print n,n1
if n1 > N:
n1 == N
statement = """ SELECT * FROM xxxxx limit %d offset %d;
"""%(n1,n)
crs.execute(statement)
var = crs.fetchall()