将db文件复制到iOS应用程序的问题

时间:2013-10-03 11:52:55

标签: ios objective-c sqlite

我有一个sqlite文件,我需要通过我的iOS应用程序阅读,但xcode在应用程序中复制它时出错... 我把sql文件放在我项目的“Supporting files”文件夹中。 这是代码:

-(NSString *)filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);

    return [[paths objectAtIndex:0] stringByAppendingPathComponent:
                                                         @"LocationsApp.sql"];
}

-(void)createEditableCopyOfDatabaseIfNeeded {
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *writableDBPath = [self filePath];
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success)
    {
        return;
    }

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"LocationsApp.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success)
    {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

这是错误:

  

2013-10-03 13:21:35.282 LaGuida [1872:60b] *断言失败 - [FirstViewController createEditableCopyOfDatabaseIfNeeded],/ Volume / Lavoro / AppleDev / LaGuida / LaGuida / FirstViewController.m:87   2013-10-03 13:21:35.284 LaGuida [1872:60b] 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法创建可写的数据库文件消息'操作无法完成。 (可可错误260.)'。'   * *第一次抛出调用堆栈:   (0x2e4f8e8b 0x387f36c7 0x2e4f8d5d 0x2eea123f 0x6b201 0x6abad 0x30c8297d 0x30c828f9 0x30daa7cf 0x30daa663 0x30daa56f 0x30c8fed5 0x30dadeb5 0x30dad333 0x30c98907 0x30c8a5a7 0x30c89d5f 0x30c89bd3 0x30c89701 0x30c86cc5 0x30cf219d 0x30ceef75 0x30ce94b9 0x30c83be7 0x30c82edd 0x30ce8ca1 0x3316476d 0x33164357 0x2e4c377f 0x2e4c371b 0x2e4c1ee7 0x2e42c541 0x2e42c323 0x30ce7f43 0x30ce31e5 0x6bbdd 0x38cecab7)   libc ++ abi.dylib:以NSException类型的未捕获异常终止

1 个答案:

答案 0 :(得分:0)

更正您的DBName

LocationsApp.sql to LocationsApp.sqlite

或您的DBName是什么