Replace a SQLite database at runtime

2019-04-02 14:18发布

问题:

I have been developing an application that required the SQLite database file to be replaced at run time, I have code to replace db but not update the database still shows that old data. please guide me.

This is for getting the file and replace

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 take a look to my code after adding your code.

I am getting the

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

what should i do now

回答1:

you have to remove the old store first:

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

this should do the trick...



回答2:

My solution to a similar problem was just to delete the file with the database:

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