2015-03-02 51 views
4

代碼如下。當有記錄要刪除時,它返回「DELETE 1」,沒有時記錄「DELETE 0」。但是,如果有記錄,它實際上並不刪除記錄。粘貼到pgAdmin III中的完全相同的SQL(用%s替換爲tournament_id)刪除記錄。我在木材/樹木階段。有任何想法嗎?python Psycopg刪除不起作用

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 

回答

5

這幾乎與您的數據庫無關。要調用Python中沒有參數的方法,請在其末尾添加()。這將調用commit您的數據庫:

DB.commit() 

而這只是得到一個方法參考:

DB.commit 

因此,在你的代碼,你實際上並沒有提交。 (或者說關閉,但這不是必要的。)

+0

非常感謝,這固定它 - 我會得到一個鋰充值。 – 2015-03-02 16:38:02