提交时出错

Error when committing

我正在使用 flask 和 psycopg2 设置一个简单的应用程序。当我尝试做一个简单的数据库更新时,我得到一个 "Internal server error",抛出一些日志记录,很明显

@app.route('/deactivate/<pid>')
def deactivate(pid):
 try:
    sql='update person set active = \'false\' where id = %s'
    logger.debug(LOGID+' running '+sql)
    cur.execute(sql,[pid])
    logger.debug(LOGID+' ran '+sql)
    cur.commit()
    logger.debug(LOGID+' commited '+sql)

加入一些日志记录,很明显它在 "commit" 中失败了 - 我无法在任何日志中找到任何解释,无论是来自 postgres 还是来自 apache。更新后的值将显示在同一应用程序内的任何调用中,因此更新非常顺利,但如果我尝试从其他任何地方查询,则更新(当然)不存在。有什么问题的线索吗?

commit 是 psycopg2 的 connection class, not the cursor class 的一个方法。由于您没有向我们展示 except 子句,我假设它正在吞下您调用 cur.commit().

时引发的 AttributeError

不知道 cur 来自哪里,我不知道您的连接对象是否在模块中可用,但它需要。然后,您需要将代码修改为如下所示:

@app.route('/deactivate/<pid>')
def deactivate(pid):
    try:
        sql = "update person set active = 'false' where id = %s"
        cur.execute(sql, [pid])
        conn.commit()
    except SomeSpecificException:
        # exception handling code here