我在 Python 脚本中运行此命令:
try:
print sql_string
cursor.execute(sql_string)
except:
print sys.exc_info()
并获得:
(<class 'psycopg2.InternalError'>, InternalError('current transaction is aborted, commands ignored until end of transaction block\n',), <traceback object at 0x1010054d0>)
但是,如果我从 psql 命令行尝试 sql_string
,它工作得很好。我知道脚本可以正常连接到数据库,因为我可以运行其他命令。
我怎样才能让 Python 为我提供更多有用的信息,说明此命令为何在脚本中失败?
最佳答案
试试这个:
try:
print sql_string
cursor.execute(sql_string)
except Exception, e:
print e.pgerror
如果您仍然收到“当前事务被中止,命令被忽略,直到事务 block 结束”,那么您的错误在您的事务中更进一步,并且该查询仅由于先前的查询失败而失败(从而使整个事务无效) ).
有关 pgerror 的详细信息可以在 http://initd.org/psycopg/docs/module.html#exceptions 的文档中找到
关于python - psycopg2.InternalError : how can I get more useful information?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3718251/