核心数据更新sqlite数据库

时间:2010-03-02 05:21:46

标签: cocoa sqlite core-data revision

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

以下是我的代码中的代码段:

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文件的名称或使用设置之外,如果你有任何东西,有没有办法说清楚你拥有的东西,并从头开始重新开始?

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

谢谢, 豪伊

1 个答案:

答案 0 :(得分:0)

您可以删除旧数据库,并以与原始数据库相同的方式复制更新附带的数据库。

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

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

相关问题