我有一个带有预加载数据的Sqlite文件。我正在使用Core Data来读取和显示Sqlite文件中的数据。
以下是我阅读数据的方式:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL;
NSString *path = [[NSBundle mainBundle] pathForResource:@"myAppname" ofType:@"sqlite"];
storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
// This dictionary automatically updates and migrates the store if it changes
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
[NSNumber numberWithBool: NO], NSReadOnlyPersistentStoreOption,
nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
if (error) {
[myErrorAlert showError:error];
}
CLS_LOG(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
但是我需要使用sqlite数据编辑器在应用程序外部执行的新数据更新Sqlite文件。
现在,当我复制粘贴并覆盖“myAppname.sqlite”文件时,出现以下错误:
CoreData: error: (14) I/O error for database at /Users/Username/Library/Application Support/iPhone Simulator/7.1/Applications/45C373D7-8A54-48EF-BA3C-2E2C6AB7467F/myAppname.app/myAppname.sqlite. SQLite error code:14, 'unable to open database file'
我确实尝试添加NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"}
,但仍然存在相同的错误。
另外一些答案建议删除对文件的引用并创建一个新的,但我不知道该怎么做。
有什么想法吗?
修改
我已经设法让它发挥作用。
在iOS 6模拟器上打开原始应用。
使用新文件Repalce Sqlite文件并运行App iOS 6模拟器而不是在iOS7模拟器上运行。没问题。
但是仍然想知道其他意见。