samedi 9 mai 2015

"attempt to write a readonly database" with FMDB after adding a new table to the sqlite db

my app worked for almost a year, I'm using FMDB from about a week now and it worked too, for few days.

The sqlite db is copied to the documents directory in appDelegate, then opened from the same directory.

Today I have just added a new table to my db and after the first build I've got this error.

For FMDB I use a FMDatabaseQueue singleton. I've also printed a log of the final db path when it's copied and when I instantiate the FMDatabaseQueue to be sure that nothing is wrong...

in appDelegate, this will happen only with a new build:

NSFileManager *fileManager = [NSFileManager defaultManager];

// remove old sqlite database from documents directory
NSURL *dbDocumentsURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"RicetteDB.sqlite"];
NSString *dbDocumentsPath = [dbDocumentsURL path];
NSLog(@"Documents path is: %@", dbDocumentsPath);

if ([fileManager fileExistsAtPath:dbDocumentsPath]) {
    NSError *error = nil;
    [fileManager removeItemAtPath:dbDocumentsPath error:&error];
    if (error) NSLog(@"Error deleting sqlite database: %@", [error localizedDescription]);
} else NSLog(@"There was no database");

// move new sqlite database from bundle to documents directory
NSString *dbBundlePath = [[NSBundle mainBundle] pathForResource:@"RicetteDB" ofType:@"sqlite"];
if (dbBundlePath) {
    NSError *error = nil;
    [fileManager copyItemAtPath:dbBundlePath toPath:dbDocumentsPath error:&error];
    if (error) NSLog(@"Error copying sqlite database: %@", [error localizedDescription]);
}  else NSLog(@"Bundle path is wrong!!!");

Instantiating the FMDB singleton:

+ (instancetype)sharedInstance {

static id sharedMyInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedMyInstance = [[self alloc] init];
});
return sharedMyInstance;

}

- (id)init {

self = [super init];
if (self) {
    // Get the documents directory
    NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *dbPath   = [docsPath stringByAppendingPathComponent:dbName];
    _databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath]; // flags:SQLITE_OPEN_READWRITE];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:dbPath]) NSLog(@"No database");

    NSLog(@"dbPath: %@", dbPath);
}
return self;

}

The logs says:

Copying the db to documents: dbPath: /var/mobile/Containers/Data/Application/xxx/Documents/RicetteDB.sqlite

Instantiate the FMDB withPath: Documents path is: /var/mobile/Containers/Data/Application/xxx/Documents/RicetteDB.sqlite

So... what's the problem? I always get: "attempt to write a readonly database" I've also tried to open the db with SQLITE_OPEN_READWRITE flag, not working, it seems the problem is with the documents folder permissions. Thank you!

Aucun commentaire:

Enregistrer un commentaire