开发者

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

开发者 https://www.devze.com 2023-01-16 19:13 出处:网络
I\'m running this command in a Python script: try: print sql_string cursor.execute(sql_string) except: print sys.exc_info()

I'm running this command in a Python script:

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开发者_Python百科 database okay, because I can run other commands.

How can I get Python to give me more useful information about why this command is failing within the script?


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


You can also tail the output of postgresql to see the query that caused the error:

$ tail -f /var/log/postgresql/postgresql.log

This is often easier than editing the script to debug it.

0

精彩评论

暂无评论...
验证码 换一张
取 消