Search code examples
iphoneresourcessqlitedatabase-permissions

sqlite3 on iPhone emulator versus device permissions?


Ok, now I finished developing on the emulator and when I installed on my test device, I got the error

attempt to write a readonly database

What are the settings to control the DB access? Is this going to be difficult to manage when deployed?

More information ... I think I know what is going on ... When I look for my DB it is not in the same path as the tutorials. What I like to do is have a group that is below the project's root folder for "Content." I added the JustADatabase.sqlite file in this group under my defined path. Since not all paths are writable, I am surmising that my user created path is not writable when installed.

I believe there is a way to use the build/copy script to install the file in the correct place.

Do you think this is the problem? Do you know how to script the fix or how to put the file in the correct location in my project so it is copied correctly? Following is the code leading up to the sqlite-open command with the 'tutorial' paths commented-out. This works for reading and may be the cause of the write access denial.

- (void)initializeDatabase {

    // ??? NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // ??? NSString *documentsDirectory = [paths objectAtIndex:0];
    // ??? NSString *pathTutorial = [documentsDirectory stringByAppendingPathComponent: @"JustaDatabase.sqlite"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"JustaDatabase" ofType:@"sqlite"]; 

    if( sqlite3_open([path UTF8String], &database) == SQLITE_OK ) {
        NSLog(@"%s: opened database %@", __FUNCTION__, path);
    } else {
        NSAssert1(0,@"Failed to open database: '%s'.", sqlite3_errmsg(database));
    }
}

Solution

  • sqlite3 permissions are just file permissions.

    you need to copy the db file to the documents directory, since you can't write to the file in the bundle directory on the device.