编写以下生成器函数是否比仅.fetchone()
在 while 循环中调用有优势?如果是这样,那将是什么优势?
谢谢。
def testf2():
db = connectToMysqlDB(None)
sql_statement = " ".join([
"select d.* ",
"from dr_snapshot d ",
"order by d.PremiseID asc, d.last_read asc; "])
sel_cur = db.cursor()
rc = sel_cur.execute(sql_statement)
loop_ok = True
while loop_ok:
meter_row = sel_cur.fetchone()
if meter_row:
yield meter_row
else:
loop_ok = False
yield None
for read_val in testf2():
print(read_val)
#Perform something useful other than print.
('610159000', 6, datetime.datetime(2012, 7, 25, 23, 0), 431900L, 80598726L)
('610160000', 6, datetime.datetime(2012, 7, 25, 23, 0), 101200L, 80581200L)
None