【问题标题】:Better way to save sqlite database file(.db) in ios application在 ios 应用程序中保存 sqlite 数据库文件(.db)的更好方法
【发布时间】:2014-04-07 15:11:49
【问题描述】:

在 ios 应用程序中复制 sqlite 数据库文件(.db)的更好方法是指在 NSBundle 或 Document 文件夹中。

【问题讨论】:

  • 如果你不想修改文件,你可以把它保存在你的bundle中,不需要将它复制到documents文件夹,如果你需要修改你需要将它复制到documents目录(因为bundle 是只读的)。如果您稍微解释一下您的意图,我们可以为您提供更多帮助。
  • @Mithun 谢谢,我想从数据库文件中读写。
  • 我之前写过this post ..也许这很有用

标签: ios iphone sqlite


【解决方案1】:

你可以试试这个

- (void)createEditableCopyOfDatabaseIfNeeded
{
    // First, test for existence.
    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DB.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success) return;

    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"KURANDB2.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-13
    • 2020-07-04
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多