【问题标题】:python Psycopg delete not workingpython Psycopg删除不起作用
【发布时间】:2015-03-02 16:20:56
【问题描述】:

代码如下。有要删除的记录时返回“DELETE 1”,没有要删除的记录时返回“DELETE 0”。但是当那里有记录时,它实际上并没有删除记录。粘贴到 pgAdmin III 中的完全相同的 SQL(%s 替换为 tournament_id)会删除记录。我在木头/树木阶段。有什么想法吗?

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

【问题讨论】:

    标签: python-2.7 psycopg2


    【解决方案1】:

    这主要与您的数据库无关。要在 Python 中调用没有参数的方法,请在其末尾添加 ()。这将在您的数据库上调用commit

    DB.commit()
    

    虽然这只是获取方法引用:

    DB.commit
    

    所以在您的代码中,您实际上并没有提交。 (或者关闭,就此而言,但这不是必需的。)

    【讨论】:

    • 非常感谢,这解决了它 - 我会得到一个锂充值。
    猜你喜欢
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多