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.
I'm using the SQLite wrapper by Matteo Bertozzi that you can find here: https://github.com/ConnorD/simple-sqlite
The database gets corrupted and spits out the error database disk image is malformed
, some queries can be run but the existing data gets messed up.
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.
I've tried these repair commands:
[sqlite executeNonQuery:@"pragma integrity_check"];
[sqlite executeNonQuery:@"reindex nodes"];
[sqlite executeNonQuery:@"reindex pristine"];
And the output was:
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`
With some further digging I found this problem: Core Data and iOS 7: Different behavior of persistent store which mentions issues with SQLite after iOS7.
Though I have no idea how to use NSPersistentStore
, so I tried running [sqlite executeNonQuery:@"pragma journal_mode = DELETE"];
and it just said SQLite Step Failed: unknown error
.
Is anyone else experiencing this, or point me in the right direction?
In the meantime I feel like this NSPersistentStore
is something I potentially should be doing.. will have to look into that.
edit:
From what I've found you only use NSPersistentStore
when the database isn't going to be updated, which mine is regularly.
Here is how I open the database:
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");
}
So I assume I need to find a way to set pragma journal_mode = DELETE
this way..?
edit 2:
I'm not convinced it's to do with the journal_mode
as I'm not using Core Data
- back to the drawing board.
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.
journal_mode
either as I'm not usingCore Data
, so it's back to square one. – Johnettajohnetteintegrity_check
etc could have potentially repaired the corruption, and thejournal_mode
was to fix a potentialCore Data
WAL
issue described in the link. – Johnettajohnettepragma
before today trying to repair the db. I did thesqlite3_threadsafe()
test mentioned here and it returned '2' which is supposed to mean it is threadsafe. – Johnettajohnette[sqlite open:writableDBPath]
bit? So if I hadsqliteThread
to use in the threads it should be fine? It's still very odd that this has just become a common issue after iOS 7.1 though. – Johnettajohnette