【问题标题】:CoreData updated sqlite databaseCoreData 更新了 sqlite 数据库
【发布时间】:2011-01-22 14:18:10
【问题描述】:

我正在使用 coredata 开发我的第一个应用程序。一切都很完美。当我在我的设备上调试应用程序(尚未在设备上安装它),然后退出应用程序,手动修改 sqlite dbase 然后再次调试应用程序时,它似乎正在使用旧版本的数据库。如果两个 sqlite 文件具有相同的名称,有没有办法告诉它只替换那里的内容?

这是我的代码中的一个 sn-p:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"mydata.sqlite"];

    /*
     Set up the store.
     For the sake of illustration, provide a pre-populated default store.
     */
     NSFileManager *fileManager = [NSFileManager defaultManager];
     // If the expected store doesn't exist, copy the default store.
     if (![fileManager fileExistsAtPath:storePath]) {
        NSLog(@"copying default from sqlite");
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydata" ofType:@"sqlite"];
         if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
         }
     }


    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

如您所见,如果它最初没有找到数据库,它会从应用程序加载 sqlite 文件。除了更改 sqlite 文件的名称或使用设置之外,有没有办法说如果你有任何东西就刷新,然后从头开始?

我担心用户购买该应用程序,然后当我发布更新时,他们仍然看到来自旧版本数据库的数据。

谢谢, 豪伊

【问题讨论】:

  • “……有没有办法说如果你有任何东西就刷新你所有的东西,然后从头开始?”如果这个存储是只读的,并且用户必须为自己的数据创建一个自己的空白存储,那没关系。否则,这是一个非常糟糕的主意。

标签: cocoa sqlite core-data revision


【解决方案1】:

您可以删除旧数据库,然后按照您最初的操作方式复制更新随附的数据库。

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Test.sqlite"];
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

您只需要一些方法来确定数据库是否已过时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多