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 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?
It is designed to perform heavily multi-threaded applications that usually create and destroy lots of cursors and make a large number of simultaneous INSERTS or UPDATES. Psycopg features client-side and server-side cursors, asynchronous communication, and notification. Psycopg 2 is both Unicode and Python 3 friendly.
Thread and process safetyThe Psycopg module and the connection objects are thread-safe: many threads can access the same database either using separate sessions and creating a connection per thread or using the same connection and creating separate cursors.
Asynchronous notificationsPsycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY.
The Cursor class of the psycopg library provide methods to execute the PostgreSQL commands in the database using python code. Using the methods of it you can execute SQL statements, fetch data from the result sets, call procedures. You can create Cursor object using the cursor() method of the Connection object/class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With