objective-cdatabasesqlitefmdb

can't write to sqlite database


My program need to operate database, and now I used 'FMDB' framework and close the sandbox in my xcode project.

but I got a trouble, it's very strange: in myself computer, read/write operate are no problem. (after archive as app), but in the other computer, it can only read, canon't write, Why? (The database is in this app's source folder).

The problem is not that the database file cannot be found, because I log the database path in other computer, It's right.

My system is "Mac OS 10.15", I'm using SDK that version '10.13' In Xcode.

Read operate: ↓

+ (charData *)getCharacteData:(NSString *)character
{

    NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"CN_Characters" ofType:@"DB"];
    FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
    [db open];

    FMResultSet *res = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM Proverbs WHERE character = '%@';",character]];
    charData *charDta = [charData new];
    if ([res next]) {
        charDta.character = [NSString stringWithString:[res stringForColumn:@"character"]];
        charDta.explanation = [NSString stringWithFormat:@"%@",[res stringForColumn:@"explanation"]];
    } else {
        return NULL;
    }
    [db close];
    return charDta;
}

Write operate: ↓

+ (BOOL)saveCharacterDataWithCharacter:(NSString *)character Explanation:(NSString *)explanation
{
    NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"CN_Characters" ofType:@"DB"];
    printf("databasePath: %s\n",databasePath.UTF8String);
    FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
    [db open];

    BOOL res = [db executeUpdate:[NSString stringWithFormat:@"UPDATE Proverbs SET explanation = '%@' WHERE character = '%@'",explanation,character]];

    [db close];
    return res;
}

Is it a permission problem?


Solution

  • When you run your app, you need to copy your file from the bundle into the documents directory.

    if([[NSFileManager defaultManager] copyItemAtPath: databasePath 
              toPath:[documentsDirectory stringByAppendingPathComponent:@"CN_Characters.DB"]
              error:&error]){
    

    Otherwise it will be read only.