【问题标题】:Replace a SQLite database at runtime在运行时替换 SQLite 数据库
【发布时间】:2012-04-04 16:06:47
【问题描述】:

我一直在开发一个需要在运行时替换 SQLite 数据库文件的应用程序,我有替换 db 但不更新数据库的代码仍然显示旧数据。请指导我。

这是为了获取文件并替换

ASIHTTPRequest *requestToDownloadDB = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.turfnutritiontool.com/dev.ios/services/turfnutritiontool_ver_two.db"]];
                // ======================================================================================================
                // All methods below remove old store from coordinator
                // ======================================================================================================

                NSError *error = nil;
                NSPersistentStore *persistentStore = [[self.persistentStoreCoordinator persistentStores] objectAtIndex:0];
                [self.persistentStoreCoordinator removePersistentStore:persistentStore error:&error];
                if (error) {
                    NSLog(@"error removing persistent store %@ %@", error, [error userInfo]);
                }

                // then update 
                //NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
                //documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"turfnutritiontool_ver_two.db"];

                NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
                NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent: @"turfnutritiontool_ver_two.db"];

                NSLog(@"This is store URL %@ ", storeURL);

                [requestToDownloadDB setDownloadDestinationPath:[NSString stringWithString:[storeURL absoluteString]]];
                [requestToDownloadDB startSynchronous];

                // add clean store file back by adding a new persistent store with the same store URL
                if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                                   configuration:nil 
                                                                             URL:storeURL 
                                                                         options:nil 
                                                                           error:&error]) {
                    NSLog(@"failed to add db file, error %@, %@", error, [error userInfo]);
                }

请在添加代码后查看我的代码。

我收到了

NSLog(failed to add db file, error (null), (null));

我现在该怎么办

【问题讨论】:

    标签: objective-c ios ios5 sqlite


    【解决方案1】:

    我对类似问题的解决方案只是删除带有数据库的文件:

    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
    

    【讨论】:

      【解决方案2】:

      您必须先删除旧商店:

      // remove old store from coordinator
      NSError *error = nil;
      NSPersistentStore *persistentStore = [[self.persistentStoreCoordinator persistentStores] objectAtIndex:0];
      [self.persistentStoreCoordinator removePersistentStore:persistentStore error:&error];
      if (error) {
          NSLog(@"error removing persistent store %@ %@", error, [error userInfo]);
      }
      
      // then update 
      NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
      NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent: @"%xyz_ver_three.db"];
      
      // add clean store file back by adding a new persistent store with the same store URL
      if (![self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                         configuration:nil 
                                                                   URL:storeURL 
                                                               options:nil 
                                                                 error:&error]) {
          NSLog(@"failed to add db file, error %@, %@", error, [error userInfo]);
      }
      

      这应该可以解决问题...

      【讨论】:

      • 我可以直接使用 NSPersistentStore 类吗,因为它给了我一个错误,对不起,但我是核心数据的新手。我是否需要创建 NSPersistentStoreCoordinator 的对象。请帮帮我
      • 我的应用程序现在不使用核心数据,只有 SQlite 用于 db 所以我需要使用或添加核心数据功能来实现这一点
      • 执行此操作后的错误消息 ver_5.1[2038:bc03] failed to add db file, error (null), (null)
      • 对不起,我以为你从一开始就在使用 CoreData。有时设置一个新的 xcode 项目并选择 coredata 比稍后添加它更容易。
      • 好吧,好吧,我什至很难理解这个复杂的过程以及我们在做什么,哈哈。但是你的答案是正确的,当你不使用 Coredata 时,我认为没有办法在运行时交换 sqlite db 的文件?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多