Rollback transactions in Sqlite

While writing my latest app Litho Reader for Narrative technologies, it became necessary to rollback a database transaction on the local database. After looking online I didn’t find a definitive guide on how to implement a rollback transaction using Sqlite.

The code changes are fairly simple to include in your database code.

  • create a connection to the database
  • create a transaction using “Begin Transaction”.
  • execute your query
  • finalize your statement!   This is important because this signals to Sqlite that your query is finished.
  • if the query was successful, then commit the transaction with “Commit Transaction”
  • on error, rollback the transaction with “Rollback transaction”
  • close your database connection.

Sqlite reference on Transactions