我想從數據庫中顯示 2 個一組的用戶列表,我可以讓它顯示前兩個,但隨后它返回到主菜單。我希望它滾動瀏覽所有數據庫,一次顯示兩行,而不是只顯示前兩行。我的代碼哪里出錯了,是否可以將 fetchmany 放在一個循環中以遍歷所有用戶?sql = ('Select * from user;') cursor.execute(sql)data = cursor.fetchmany(size=2)for row in data: print (row["userID"],":", row["username"], ":", row["age"])
1 回答

白豬掌柜的
TA貢獻1893條經驗 獲得超10個贊
已編輯:您可以使用fetchall方法獲取靜態列表并遍歷它。
sql = ('Select * from user;')
cursor.execute(sql)
data = cursor.fetchall()
for i in range(0, len(data), 2):
for row in data[i:i+2]:
print(row["userID"],":", row["username"], ":", row["age"])
input() # to display two rows per enter key press
添加回答
舉報
0/150
提交
取消