python-2.7 - python Psycopg删除不起作用

标签 python-2.7 psycopg2

代码如下。当有要删除的记录时返回“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 中调用没有参数的方法,请添加 ()到最后。这将调用 commit在您的数据库上:

DB.commit()

而这只得到一个方法引用:
DB.commit

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

关于python-2.7 - python Psycopg删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814545/

相关文章:

python - Tweepy - 排除转推

python - 如何通过python打开文件夹名称为正则表达式的文件?

python - 在 Psycopg2 中高效地插入大量行

postgresql - 使用 psycopg cur.execute 创建 postgres 模式

python - cur.executemany 在字符串格式化期间并非所有参数都已转换

python - 如何在 kivy 的微调器中设置按钮大小?

python - 强制在所有继承类中实现一个方法

python-2.7 - 如何计算骰子系数以测量python中图像分割的准确性

python - Mac 上的错误 : No module named psycopg2. 扩展

python - 多线程 python psycopg2