0

I can I update multiple columns for a particular row with psycopg2? I have tried this but I get the error I show bellow:

cursor = postgres_conn.cursor()
sql_update = "UPDATE table_name SET a=%s, b=%s, c=%s WHERE url=%s"
cursor.execute(sql_update,(1,2,3,url))


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
Miguel
  • 2,738
  • 3
  • 35
  • 51
  • Looks like there had been another command before this that failed and you need to roll back the connection, `postgres_conn.rollback()`, before preceding with the above. – Adrian Klaver Jan 09 '21 at 17:33
  • Thanks, it worked! I need to execute this periodically. Is there a way to check if I need a rollback? Or I just execute this rollback as insurance and proceed. – Miguel Jan 09 '21 at 17:38
  • 1
    See [Transactions](https://www.psycopg.org/docs/usage.html#transactions-control) or use `try/except` and if the error pops do a rollback and retry the query. – Adrian Klaver Jan 09 '21 at 17:52
  • If there was an error that left the connection in need of a rollback, then it must have raised an exception. If that didn't stop your program, this means there is a try/except somewhere that caught the exception but didn't do what it should do, ie handle the error and rollback. – bobflux Jan 09 '21 at 20:05

0 Answers0