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.
精彩评论