Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python Psycopg delete not working

The code is below. It returns "DELETE 1" when there is a record to delete and "DELETE 0" when there isn't. But it doesn't actually delete the record when there is one there. The exact same SQL (with %s replaced with tournament_id) pasted into pgAdmin III deletes the record. I'm at the wood/trees stage. Any ideas?

def deletePlayers(tournamentId):
    # takes tournamentId and deletes all records from tournament_player 
    # with that tournamentId 
    # but not an error if no records to delete

    # check tournamentId set if not return
    if tournamentId < 1:
        returnStr = "ERROR - tournamentId not set"
        return returnStr

    DB = connect()
    cur = DB.cursor()

    # delete data
    SQL = "DELETE from tournament_player where tournament_id = %s;"
    data = (tournamentId,)
    cur.execute(SQL, data )
    returnStr = cur.statusmessage
    DB.commit
    # tidy up
    cur.close
    DB.close

    return returnStr
like image 369
Campbell Boyd Avatar asked Mar 02 '15 16:03

Campbell Boyd


1 Answers

This is mostly unrelated to your database. To call a method in Python that has no arguments, add () to the end of it. This will call commit on your database:

DB.commit()

while this only gets a method reference:

DB.commit

So in your code, you didn't actually commit. (Or close, for that matter, but that's not necessary.)

like image 74
Dan Getz Avatar answered Sep 20 '22 09:09

Dan Getz