title: Should I put my faith in SQLite transactions to avoid file corruption?
body: <h2>Short version</h2>
  
  <p>If my process is terminated in the middle of a transaction, or while SQLite is committing a transaction, what are the chances that the database file will be corrupted?</p>
  
  <h2>Long version</h2>
  
  <p>My application uses an SQLite database for storage (directly, not via Core Data). I'm working on a new version of the application which will require an update to the database schema. On launch, the app will check the database and, if it needs updating, execute a series of SQL statements to do so.</p>
  
  <p>Depending on the amount of data in the database, the update may be long running (on the order of seconds), so I need to consider the possibility that the process may be terminated before the update is completed. (For context, this on an iPhone, where the processor is slow and the app may be terminated by an incoming phone call.) I will, of course, wrap the upgrade SQL statements in a transaction. Will that be enough to guarantee that the database will not be corrupted?</p>
  
  <p>I'm assuming that transactions work as advertised, and that if the process is terminated in the middle of the transaction, the file will be OK. But I'm also assuming there is a window of time during the COMMIT where something can go wrong.</p>
  
  <p>To play it safe, I <em>could</em> create a backup copy of the database file before starting the update, but if the transactions are safe then that would be overkill. It would also make the update process take longer, which increases the chance it would be interrupted, and then I'd have to consider that the file copy operation might be interrupted... I'd like to keep the code as simple as possible (but no simpler).</p>
  
  <p>In the course of researching this question I've started reading "<a href="http://sqlite.org/atomiccommit.html" rel="noreferrer">Atomic Commit In SQLite</a>", which is more detail than I probably need to know, but is giving me faith that I don't need to second-guess SQLite's ability to protect the database file. But I'd still like to hear from Stack Overflow: <strong>is a transaction good enough, or should I be more cautious?</strong></p>