python - psycopg2.InternalError : how can I get more useful information?

标签 python postgresql

我在 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/

相关文章:

python - 打包一个要在控制台运行的Python项目 : pyproject. toml文件结构

python - 如何在 flask 资源中测试 session

python - python 中重写 __str__ 方法后如何获取实例的地址

java - JPA,Spring映射多个实体

postgresql - 在postgresql中实现自定义比较

python - 安装 pyspider - "python setup.py egg_info"失败,错误代码为 1

python - python中奇怪的换行符错误

ruby-on-rails - Rails 控制台忽略 ActiveRecord :Relation object? 中的换行符

PostgreSQL:匿名 DO block 的 void 返回类型

java - Mybatis If 语句使用 include 属性