gpt4 book ai didi

objective-c - 在运行时替换SQLite数据库

转载 作者:行者123 更新时间:2023-12-03 16:27:00 34 4
gpt4 key购买 nike

我一直在开发一个需要在运行时替换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));

我现在应该怎么做

最佳答案

您必须先删除旧商店:

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

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

关于objective-c - 在运行时替换SQLite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782588/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com