Currently, I have the following method to execute INSERT/UPDATE/DELETE statements using psycopg2
in Python
:
def exec_statement(_cxn, _stmt):
try:
db_crsr = _cxn.cursor()
db_crsr.execute(_stmt)
_cxn.commit()
db_crsr.close()
return True
except:
return False
But what I would really like it to do, instead of bool, is return the row count affected by the transaction or -1 if the operation fails.
Is there a way to get a number of rows affected by _cxn.commit()
? E.g. for a single INSERT it would be always 1, for a DELETE or UPDATE, the number of rows affected by the statement etc.?