title: SQLite "database disk image is malformed"
body: <p>I am having trouble with an app where the SQLite database is getting corrupted.  There was the odd case of this previously, but it seems to have become a lot more common after the release of iOS 7.1.</p>
  
  <p>I'm using the SQLite wrapper by <strong>Matteo Bertozzi</strong> that you can find here: <a href="https://github.com/ConnorD/simple-sqlite" rel="nofollow noreferrer">https://github.com/ConnorD/simple-sqlite</a></p>
  
  <p>The database gets corrupted and spits out the error <code>database disk image is malformed</code>, some queries can be run but the existing data gets messed up.</p>
  
  <p>I have searched high and low and can't find a solution, I'm hoping someone here has some ideas since this is becoming a more common issue after the iOS update.</p>
  
  <p>I've tried these repair commands:</p>
  
  <pre><code>[sqlite executeNonQuery:@"pragma integrity_check"];
  [sqlite executeNonQuery:@"reindex nodes"];
  [sqlite executeNonQuery:@"reindex pristine"];
  </code></pre>
  
  <p>And the output was:</p>
  
  <pre><code>SQLite Step Failed: database disk image is malformed
  SQLite Prepare Failed: unable to identify the object to be reindexed
   - Query: reindex nodes
  SQLite Prepare Failed: unable to identify the object to be reindexed
   - Query: reindex pristine`
  </code></pre>
  
  <p>With some further digging I found this problem: <a href="https://stackoverflow.com/questions/18870387/core-data-and-ios-7-different-behavior-of-persistent-store/18870738#18870738">Core Data and iOS 7: Different behavior of persistent store</a> which mentions issues with SQLite after iOS7.</p>
  
  <p>Though I have no idea how to use <code>NSPersistentStore</code>, so I tried running <code>[sqlite executeNonQuery:@"pragma journal_mode = DELETE"];</code> and it just said <code>SQLite Step Failed: unknown error</code>.</p>
  
  <p>Is anyone else experiencing this, or point me in the right direction?  </p>
  
  <p>In the meantime I feel like this <code>NSPersistentStore</code> is something I potentially should be doing.. will have to look into that.</p>
  
  <p><strong>edit:</strong></p>
  
  <p>From what I've found you only use <code>NSPersistentStore</code> when the database isn't going to be updated, which mine is regularly.</p>
  
  <p>Here is how I open the database:</p>
  
  <pre><code>sqlite = [[Sqlite alloc] init];
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"HomeOpenDatabase8.sql"];
  
  if (![sqlite open:writableDBPath]) {
      NSLog(@"DB Not Writable");
      return;
  } else {
      NSLog(@"All good");
  }
  </code></pre>
  
  <p>So I assume I need to find a way to set <code>pragma journal_mode = DELETE</code> this way..?</p>
  
  <p><strong>edit 2:</strong></p>
  
  <p>I'm not convinced it's to do with the <code>journal_mode</code> as I'm not using <code>Core Data</code> - back to the drawing board.</p>
  
  <p>The biggest flag to me is this error popping up so soon after iOs 7.1 was released, surely it can't be a coincidence.. I'll continue trying to replicate the issue on my device.</p>