如何在 psycopg2 中回滚?
How to rollback in psycopg2?
我在 qry2 中强加了一个错误,这样 运行 qry3 假设它可以回滚到 save_1,但事实并非如此。有收获吗?使用 PostgreSql 14.2
qry1 = ('begin;' +
'savepoint save_1;' +
'delete from ntnb_cup;')
qry2 = ...
# Force error
qry3 = 'rollback to save_1;'
try:
cursor = conn.cursor()
cursor.execute(qry1)
conn.commit()
except Exception as err:
cursor.close()
conn.close()
exit()
try:
cursor.execute(qry2)
conn.commit()
except Exception as err:
cursor.execute(qry3)
conn.commit()
finally:
cursor.close()
conn.close()
try:
cursor = conn.cursor()
cursor.execute(qry1)
conn.commit()
一旦你承诺,你就是承诺。要保留稍后回滚到保存点的选项,请不要在此处提交。
我在 qry2 中强加了一个错误,这样 运行 qry3 假设它可以回滚到 save_1,但事实并非如此。有收获吗?使用 PostgreSql 14.2
qry1 = ('begin;' +
'savepoint save_1;' +
'delete from ntnb_cup;')
qry2 = ...
# Force error
qry3 = 'rollback to save_1;'
try:
cursor = conn.cursor()
cursor.execute(qry1)
conn.commit()
except Exception as err:
cursor.close()
conn.close()
exit()
try:
cursor.execute(qry2)
conn.commit()
except Exception as err:
cursor.execute(qry3)
conn.commit()
finally:
cursor.close()
conn.close()
try:
cursor = conn.cursor()
cursor.execute(qry1)
conn.commit()
一旦你承诺,你就是承诺。要保留稍后回滚到保存点的选项,请不要在此处提交。