Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling and RollBack Transaction in SQLITE from SQL Statement

Tags:

sqlite

I'm Altering multiple sqlite tables with SQL script by calling ExecuteNonQuery. I want to do this operation in transaction and want to roll it back when anything fails.

I looked at BEGIN TRANSACTION and its clear that I have to call ROLLBACK TRANSACTION when anything goes wrong. But I don't know how could TRY...CATCH (Transact-SQL) kind of thing here.

NOTE: Whole of Sql Script file (which contains many other statements apart from these few statements which needs to be fired in one transaction) is read by .ReadToEnd() and then executed in one go as of now. I want to handle this in sql script file itself and don't want to change the code.

like image 362
IsmailS Avatar asked Mar 15 '11 06:03

IsmailS


People also ask

What is ROLLBACK in SQLite?

ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.

How do I use transactions in SQLite?

SQLite transaction statements First, open a transaction by issuing the BEGIN TRANSACTION command. After executing the statement BEGIN TRANSACTION , the transaction is open until it is explicitly committed or rolled back. Second, issue SQL statements to select or update data in the database.

Does SQLite support transaction?

SQLite supports multiple simultaneous read transactions coming from separate database connections, possibly in separate threads or processes, but only one simultaneous write transaction. A read transaction is used for reading only. A write transaction allows both reading and writing.

Does SQLite support nested transactions?

"Nested Android Transactions" do not use SQLites nested transaction/savepoint support. Rather a nested Android transaction suppresses manifesting a SQLite transaction. The nested transaction cannot be rolled back itself, because it does not exist apart from the outside transaction.


1 Answers

Please take a look at SQLite on conflict clause

Start with BEGIN TRANSACTION

You have to add ON CONFLICT ROLLBACK on your actions

And COMMIT TRANSACTION at the end ;-)

like image 83
ralf.w. Avatar answered Sep 23 '22 15:09

ralf.w.