0

I have a thread that runs independently

thread.start_new_thread(listeningTF2Servers, ())

which calls up this method eventually:

    def updateStats():
        ...
        for player in pastGames[i]['players']:
        ...
                        cursor.execute('SELECT yes FROM newstats WHERE nick = \'' + player['nick'] + '\' ORDER BY totalgames DESC LIMIT 1')
                        for row in cursor.fetchall():
                            if row[0] == 1:
                                if scoreDict[player['team']] == 1:
                                    cursor.execute('UPDATE newstats SET totalgames = totalgames + 1, wins = wins + 1, medicgames = medicgames + 1, medicwins = medicwins +1 WHERE nick = \'' + lower(player['nick']) + '\'')
                                    cursor.execute('COMMIT;')
                                else:
                                    #similar query
                            else:
                                if scoreDict[player['team']] == 1:
                                    cursor.execute('UPDATE newstats SET totalgames = totalgames + 1, wins = wins + 1 WHERE nick = \'' + lower(player['nick']) + '\'')
                                    cursor.execute('COMMIT;')
                                else:
                                    #similar query
       ...

Here's the thing. Everything else in the thread that comes before this method works fine.

But updateStats() seems to work... weirdly. But about HALF of the players that have to get updated do not seem to get updated in the database.

For example, after every game this method is called so that players' game stats and win stats are incremented. But for some group of players, this NEVER happens, even though for all other players who played the SAME GAME the stats get updated properly.

Is there an issue with my code that causes issues with threading or postgres? Or is there something with SQL or postgres or Python that I'm not aware of that causes these issues?

2
  • I really don't want to try that... Commented Aug 20, 2013 at 4:26
  • My point is: Never pass variables through string manipulation into a query. This opens the gates to hell and the nether realms and awakens Him Who Waits Behind The Wall. Let your driver take care of this. I.E. instead of execute ('select 1 where a=\'' + x + '\'') use execute ('select 1 where a=%s', [x]). Commented Aug 20, 2013 at 4:58

1 Answer 1

1

Just an educated guess: To the first query you pass player['nick'], but to the other calls lower(player['nick']) although all queries affect newstats.nick. So my guess is that the results will be different depending on whether a player's nick is all lower-case or not.

You should try something like this:

lowerNick = lower(player['nick'])
...
cursor.execute('SELECT yes FROM newstats WHERE nick = %s ORDER BY totalgames DESC LIMIT 1', [lowerNick] )
...
cursor.execute('UPDATE newstats SET totalgames = totalgames + 1, wins = wins + 1, medicgames = medicgames + 1, medicwins = medicwins +1 WHERE nick = %s', [lowerNick])
...
cursor.execute('UPDATE newstats SET totalgames = totalgames + 1, wins = wins + 1 WHERE nick = %s', [lowerNick])
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah... I just noticed that. I think that might be the issue, I'll double check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.