postgresql psycopg2.InternalError:我怎样才能获得更多有用的信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3718251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
psycopg2.InternalError: how can I get more useful information?
提问by AP257
I'm running this command in a Python script:
我在 Python 脚本中运行此命令:
try:
print sql_string
cursor.execute(sql_string)
except:
print sys.exc_info()
and getting:
并获得:
(<class 'psycopg2.InternalError'>, InternalError('current transaction is aborted, commands ignored until end of transaction block\n',), <traceback object at 0x1010054d0>)
However if I try the sql_string
from the psql command line, it works just fine. I know the script is connecting to the database okay, because I can run other commands.
但是,如果我sql_string
从 psql 命令行尝试,它工作得很好。我知道脚本可以正常连接到数据库,因为我可以运行其他命令。
How can I get Python to give me more useful information about why this command is failing within the script?
如何让 Python 为我提供有关此命令在脚本中失败的原因的更多有用信息?
采纳答案by Matthew Wood
Try this:
尝试这个:
try:
print sql_string
cursor.execute(sql_string)
except Exception, e:
print e.pgerror
If you are still getting "current transaction is aborted, commands ignored until end of transaction block" then your error is further back in your transaction and this query is only failing due to a previous query failing (and thereby invalidating the entire transaction).
如果您仍然收到“当前事务被中止,命令被忽略,直到事务块结束”,那么您的错误会进一步回到您的事务中,并且此查询仅由于先前的查询失败(从而使整个事务无效)而失败。
The details for pgerror can be found in the documentation at http://initd.org/psycopg/docs/module.html#exceptions
pgerror 的详细信息可以在http://initd.org/psycopg/docs/module.html#exceptions的文档中找到
回答by nathancahill
You can also tail the output of postgresql to see the query that caused the error:
您还可以拖尾 postgresql 的输出以查看导致错误的查询:
$ tail -f /var/log/postgresql/postgresql.log
This is often easier than editing the script to debug it.
这通常比编辑脚本来调试更容易。