我正在将赌场作为一个学校项目,并且我正在为玩家提供一种查看他们最近 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())
但是,正如您可能认为的那样,这会给我 1 个变量的所有结果,这不是我想要的。我想将每一项(P1_结果、P2_结果等)一次附加到一个列表中,这样我就可以迭代它并显示相应的结果。
通过这样做,我可以非常低效地实现这一点
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())
但是,由于我想展示最近 5 场比赛,因此台词太多,无法证明其合理性。 知道我该怎么做吗?我对 sqlite 很陌生,所以如果我的代码很糟糕,我深表歉意
您可以使用
[*playeroutcomes] = cursor.fetchone()
将结果解压到列表中