Flask 提交时出错
我正在使用flask和psycopg2设置一个简单的应用程序。当我尝试做一个简单的数据库更新时,我得到一个“内部服务器错误”,加上一些日志记录,很明显Flask 提交时出错,flask,psycopg2,Flask,Psycopg2,我正在使用flask和psycopg2设置一个简单的应用程序。当我尝试做一个简单的数据库更新时,我得到一个“内部服务器错误”,加上一些日志记录,很明显 @app.route('/deactivate/<pid>') def deactivate(pid): try: sql='update person set active = \'false\' where id = %s' logger.debug(LOGID+' running '+sql) cur.
@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)
@app.route(“/deactivate/”)
def停用(pid):
尝试:
sql='update person set active=\'false\'其中id=%s'
logger.debug(LOGID+'running'+sql)
当前执行(sql,[pid])
logger.debug(LOGID+'ran'+sql)
cur.commit()
logger.debug(LOGID+'committed'+sql)
加上一些日志,很明显它在“提交”中失败了——我在任何日志中都找不到任何解释,无论是从postgres还是apache。更新后的值将显示在同一应用程序中的任何调用中,因此更新显然进行得很顺利,但是如果我尝试从其他任何地方查询,则更新if(of cource)不在那里。有什么问题的线索吗?
commit
是psycopg2的一种方法,而不是最简单的方法。由于您没有向我们显示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
@app.route(“/deactivate/”)
def停用(pid):
尝试:
sql=“更新人员集活动='false',其中id=%s”
当前执行(sql,[pid])
康涅狄格州提交
除了一些特殊的例外:
#这里的异常处理代码
cur来自哪里?你确定你正在查询应用程序正在使用的同一个数据库吗?当然。我可以在其他地方更新数据库并查看更新,但我无法提交更改。。。