在运行时替换SQLite数据库 [英] Replace a SQLite database at runtime
本文介绍了在运行时替换SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在开发一个应用程序,需要在运行时替换SQLite数据库文件,我有代码替换db但是没有更新数据库仍然显示旧数据。请指导我。
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在添加您的代码后查看我的代码。
Pelase take a look to my code after adding your code.
我得到了
NSLog(failed to add db file, error (null), (null));
我现在该怎么办
推荐答案
您必须先删除旧商店:
// 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...
这篇关于在运行时替换SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文