3
我想編寫一個生成器函數,該函數將在內存有限的系統上運行,該系統使用PyMySql(或MySQLDb)一次返回一個選擇查詢的結果。下面的作品:在內存有效的生成器中使用PyMySql的正確方法
#execute a select query and return results as a generator
def SQLSelectGenerator(self,stmt):
#error handling code removed
cur.execute(stmt)
row = ""
while row is not None:
row = self.cur.fetchone()
yield row
但是,下面也似乎工作,但是它是否執行fetchall()是神祕的。我不能Python的DB API當你迭代的光標對象名單究竟發生在發現:
#execute a select query and return results as a generator
def SQLSelectGenerator(self,stmt):
#error handling code removed
cur.execute(stmt)
for row in self.cur:
yield row
在這兩種情況下,下面的打印所有行成功地
stmt = "select * from ..."
for l in SQLSelectGenerator(stmt):
print(l)
所以我想知道第二個實現是好還是壞,以及它是調用fetchall還是用fetchone做一些棘手的事情。因爲有數百萬行,Fetchall會炸燬這個系統。