我正在制作一个赌场作为学校项目,我正在制作一种让玩家观看他们最后5场比赛的方式, 到目前为止,我的代码如下所示
cursor.execute(f"""SELECT P1_Outcome, P2_Outcome, P3_Outcome, P4_Outcome, P5_Outcome FROM Game_History WHERE username = '{username}' ORDER BY RecordID DESC LIMIT 5 """)
playeroutcomes = (cursor.fetchone())
然而,正如你可能假设的那样,这会给出一个变量的所有结果,这不是我想要的.我想要将每个项目(P1_Outputment、P2_Output等)逐个附加到一个列表中,这样我就可以遍历它并显示各自的结果.
通过这样做,我可以非常低效地实现这一点
cursor.execute(f"""SELECT P1_Outcome FROM Game_History WHERE username = '{username}'""")
playeroutcomes.append(cursor.fetchone())
cursor.execute(f"""SELECT P2_Outcome FROM Game_History WHERE username = '{username}'""")
playeroutcomes.append(cursor.fetchone())
cursor.execute(f"""SELECT P3_Outcome FROM Game_History WHERE username = '{username}'""")
playeroutcomes.append(cursor.fetchone())
cursor.execute(f"""SELECT P4_Outcome FROM Game_History WHERE username = '{username}'""")
playeroutcomes.append(cursor.fetchone())
cursor.execute(f"""SELECT P5_Outcome FROM Game_History WHERE username = '{username}'""")
playeroutcomes.append(cursor.fetchone())
然而,由于我想展示过go 5场比赛,这将是太多的线来证明. 知道我该怎么做吗我对sqlite非常陌生,所以如果我的代码很烂,我道歉.