I am trying to build a PostgreSQL database with Python code that will simulate a tournament. The players table contains four columns - name, id, wins, matches.
The reportMatch() function takes in two arguments, the ids of the winner and the loser of a particular match and updates the statistics in the database. It will increment the "wins" of the winner by 1, and the "matches" of both the players by 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)
I know I shouldn't set the wins and matches to 1, since it's not incrementing the current value, but the database currently has no matches. So, the first time I run it, setting the value to 1 works temporarily.
The above function is called through a client code function, testReportMatches():
def testReportMatches():
registerPlayer("Bruno Walton")
registerPlayer("Boots O'Neal")
registerPlayer("Cathy Burton")
registerPlayer("Diane Grant")
standings = playerStandings()
[id1, id2, id3, id4] = [row[1] for row in standings]
reportMatch(id1, id2)
reportMatch(id3, id4)
standings = playerStandings()
for (n, i, w, m) in standings:
if m != 1:
raise ValueError("Each player should have one match recorded.")
if i in (id1, id3) and w != 1:
raise ValueError("Each match winner should have one win recorded.")
elif i in (id2, id4) and w != 0:
raise ValueError("Each match loser should have zero wins recorded.")
print "7. After a match, players have updated standings."
registerPlayer() is used to insert a new player into the players database. playerStandings() is used to get a list of tuples of all the players.
The problem that I am running into is the update query in reportMatch(), which doesn't seem to work. I tried printing out standings before and after the two calls to reportMatch() in testReportMatches(), but both of them have matches and wins as 0. Somehow, the matches and wins in the database are not being updated.