0

I have an app that inserts and or updates multiple (100's) of rows into a table. There's only one thread running at a time in my app. Each row is being calculated and checked before each insert/update in several functions.

I'm concerned that if the app gets interrupted between the start of inserting the rows and the end my data will be corrupted/incomplete. Is this possible to happen?

I tried several times to interrupt the process with the home key, it didn't but this is not exhaustive approach.

I'm am implementing the singleton method discussed here: Is Sqlite Database instance thread safe but this does not address my question.

Community
  • 1
  • 1
rnorman3
  • 49
  • 1
  • 8
  • 1
    Use a [transaction](https://www.sqlite.org/lang_transaction.html) around all UPDATEs and INSERTs. –  Apr 30 '15 at 14:34
  • a transaction around all the updates and inserts will not work if i'm inserting 200+ rows separately. I figured the right way to do it (I think). Put all the row information into an arraylist, then put a loop between the start and end transaction to insert all the rows. Thanks for your comment though, it pointed me in the right direction. – rnorman3 Apr 30 '15 at 17:49
  • Why do you accept the answer that recommends transactions if you don't want to use a transaction? –  May 01 '15 at 09:42

1 Answers1

1

From the fine manual . . .

A transactional database is one in which all changes and queries appear to be Atomic, Consistent, Isolated, and Durable (ACID). SQLite implements serializable transactions that are atomic, consistent, isolated, and durable, even if the transaction is interrupted by a program crash, an operating system crash, or a power failure to the computer.

If you are executing multiple hundreds of SQL statements, and you need to know that either a) all of them committed, or b) none of them committed, wrap all those SQL statements within a single transaction.

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185