2015-03-02 62 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