FMDB-Unknown error calling sqlite3_step (8: attempt to write a readonly

FMDB

报错误:

Unknown error calling sqlite3_step (8: attempt to write a readonly

现象:

对sqllite数据库做写入操作,发现模拟器正常,真机情况下无法写入。

原因:

项目中的sqllite数据库资源会被放在资源文件夹,而此资源文件夹是只读的。

解决方案:

程序第一次启动初始化时候把需要写入的sqllite数据库文件复制到NSDocumentDirectory

 if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        [self copyDBFile:@"empty.db"];
    }

- (void)copyDBFile:(NSString *)dbStr{
    NSString *dbPath = [[NSBundle mainBundle] pathForResource:dbStr ofType:nil];
    
    NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path = [paths safeObjectAtIndex:0];
    NSString *finalPath = [path stringByAppendingPathComponent:dbStr];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation]){
         [[NSFileManager defaultManager] copyItemAtPath:dbPath toPath:finalPath error:NULL];
    }
}

你可能感兴趣的:(FMDB-Unknown error calling sqlite3_step (8: attempt to write a readonly)