I want to execute a script in Sqlite3 from Python, and I want the entire script to be rolled back if it does not entirely succeed. Is this possible?
Here is what I've tried:
sql = open('my_file').read()
try:
conn.executescript(sql)
conn.commit()
except sqlite3.OperationalError as oe:
print('Failure to execute {0}: {1}'.format(f, oe))
conn.rollback()
exit(1)
But this doesn't work. When an error is thrown, the commit
is not called, as it goes to the except
block. Nevertheless, the parts of the script that worked persist to the database.