【问题标题】:Problems copying db file to iOS app将 db 文件复制到 iOS 应用程序时出现问题
【发布时间】:2013-10-03 11:52:55
【问题描述】:

我有一个我的 iOS 应用程序需要读取的 sqlite 文件,但是 xcode 在应用程序中复制它时出错... 我将 sql 文件放在项目的“支持文件”文件夹中。 代码如下:

-(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],/Volumes/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 类型的未捕获异常终止

【问题讨论】:

  • Cocoa 错误 260:没有这样的文件,看起来您尝试从包中复制的文件不存在。尝试记录defaultDBPathwritableDBPath
  • 已解决...扩展名错误,谢谢

标签: ios objective-c sqlite


【解决方案1】:

更正您的 DBName

LocationsApp.sql to LocationsApp.sqlite

或者你的 DBName 是什么

【讨论】:

  • 我太笨了,分机错误...文件名是 LocationsApp.db 你给了我一个重要的线索,谢谢 ;)
  • @iMove 你可以随意命名文件,SQLite 仍然会加载它。没有指定 SQLite 文件必须具有扩展名 .sqlite
  • @rckoenes 我只是通过这个特定的问题,其中给出的错误是“没有这样的文件”,这就是为什么我只是给他一个想法
  • @Michele 永远欢迎你,老兄,你解决了你的问题,我很高兴..:)
  • @iMove 我知道我可以给我的 db 文件名提供任何类型的扩展名,但问题是我写了一个 .sql 扩展名而不是 .db(我的文件有)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多