我正在尝试用Python代码构建一个PostgreSQL数据库,它将模拟锦标赛。players表包含四列:name、id、wins、matches。在
reportMatch()
函数接受两个参数,即特定匹配的赢家和输家的ID,并更新数据库中的统计信息。它将增加胜利者的“胜利”1,并增加两个玩家的“匹配”1。在
def reportMatch(winner, loser):
conn = connect()
c = conn.cursor()
SQL = 'update players set wins = 1 where id = %s;'
data = (winner, )
c.execute(SQL, data)
SQL = 'update players set matches = 1 where id = %s or id = %s;'
data = (winner, loser)
c.execute(SQL, data)
我知道我不应该将wins和matches设置为1,因为它不会增加当前值,但数据库当前没有匹配项。所以,我第一次运行它时,将值设置为1是暂时有效的。在
以上函数是通过客户端代码函数testReportMatches()
调用的:
registerPlayer()
用于将新播放器插入播放机数据库。playerStandings()
用于获取所有玩家的元组列表。在
我遇到的问题是reportMatch()
中的更新查询,它似乎不起作用。我尝试打印出在testReportMatches()
中对reportMatch()
的两个调用前后的排名,但它们都有匹配项,并且都以0获胜。不知何故,数据库中的比赛和胜利没有被更新。在
您需要在} 提交事务。在
reportMatch
函数的末尾使用^{见psycopg2 usage。在
相关问题 更多 >
编程相关推荐