【发布时间】: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