2012-03-20 49 views
1

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

这是获取该文件并替换

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]); 
       } 

Pelase看看我的代码加入你的代码之后。

我收到

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

我应该现在做的

回答

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]); 
} 

这应该做的伎俩...

+0

我可以直接使用NSPersistentStore类,因为它给我一个错误,抱歉,但我是新的核心数据。我需要创建NSPersistentStoreCoordinator的对象吗?请帮助我 – Retro 2012-03-20 08:13:14

+0

我的应用程序现在不使用核心数据,只有SQlite使用db,所以我需要使用或添加核心数据功能来实现这个 – Retro 2012-03-20 08:16:04

+0

我添加CoreData lib并创建对象?它会工作 – Retro 2012-03-20 08:29:51

0

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

[[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error]; 
相关问题