我想从数据库中以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"])
答案 0 :(得分:1)
已编辑:
您可以使用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