[英]Web.py sql query, why we can only traverse the result at the first time?
這是我的代碼:
import web
user_db = web.database(dbn='mysql', ....)
info_list = user_db.query("select * from tablename where t_id= ")
for info in info_list:
# work ok at first time, print the correct id
print info.id
for info in info_list:
# Code can't reach here
print info.id
每個第二次似乎不起作用。 為什么?
根據源代碼 ,底層的query()
調用返回一個迭代器 ,該迭代器在第一個循環后將耗盡。
如果需要多次遍歷,請將其轉換為列表:
info_list = list(user_db.query("select * from tablename where t_id= "))
或者,您可以使用itertools.tee()
創建新的迭代器:
info_list1, info_list2 = itertools.tee(info_list)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.