没有 Z_METADATA 的 xCode Sqlite 数据库创建
当我的 sqlite 数据库是使用核心数据模型创建时,在这一行:
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
我收到错误:
NSUnderlyingException=I/O SQLite error code:1, 'no such table: Z_METADATA'
知道如何解决这个问题吗?我已经尝试了好几天了。数据库已创建并复制到我设备上的文档目录中。
注意:
如果我删除应用程序,重建并安装在我的设备上 - .sqlite 文件的日期是两天前,.mom 文件的日期是昨天。如果需要的话,数据库是否不会在编译时重新创建?我的项目中没有 .sqlite 文件,只有 .xcdatamodel。
感谢您抽出时间。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
//\MOVES DATABASE TO NEW LOCATION
NSString *storePath = [documentsDirectory stringByAppendingPathComponent:@"myShoeDatabase.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn’t exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"myShoeDatabase" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
enter code here
When my sqlite database is created using a core data model, at this line:
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
I get an error:
NSUnderlyingException=I/O SQLite error code:1, 'no such table: Z_METADATA'
Any idea how to fix this? I've been trying for days. The database is created and copied into the documents directory on my device.
Note:
If I DELETE the application, rebuild, and install on my device - the .sqlite file is dated two days ago and the .mom file is dated yesterday. Is the database not recreated at compile time if necessary? I have no .sqlite file in my project, just the .xcdatamodel.
Thanks for your time.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
//\MOVES DATABASE TO NEW LOCATION
NSString *storePath = [documentsDirectory stringByAppendingPathComponent:@"myShoeDatabase.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn’t exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"myShoeDatabase" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
enter code here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请从这个基本的完整性检查开始:将数据库带到您的 Mac,使用 sqlite3.exe 打开它,并查看该表是否存在。
如果是,那么您的程序指向错误的数据库。
如果不是,那么您的表从未创建过(或者数据库已损坏,但这将是令人惊讶的)。
Please start with this basic sanity check: Bring the database to your Mac, open it with sqlite3.exe, and see if the table is present.
If it is, then your program is pointing to the wrong database.
If it is not, then your table was never created (or the database is corrupt as hell, but that would be surprising).
Core Data 在打开存储时似乎首先查找元数据,因此有关 Z_METADATA 的投诉通常表明文件已损坏或格式错误。我怀疑复制的文件有问题(权限、损坏等)或商店的
选项
有问题。我建议:
Core Data seems to look for the metadata first when it opens a store so a complaint about the Z_METADATA usually indicates a corrupted file or a file in the wrong format. I would suspect either a problem with the copied file (permission, corruption etc) or a problem with the
options
for the store.I would suggest:
nil
options dict to make sure that is not the problem.首先,您需要捕获这些错误并至少报告它们:
此外,该方法返回成功或失败的 BOOL,您也应该注意这一点。
除此之外,假设 sqlite 文件有效,则代码没有任何明显的错误。您可以在示例应用程序中复制此内容吗?如果是这样,您可以在问题中发布指向它的链接,我很乐意修改它。如果事实证明这是 Apple 的错误,那么您将为您的雷达准备好测试用例。如果它在测试中没有重复,那么您可以比较差异以查看原始应用程序中存在什么问题。
First, you need to catch these errors and at least report them:
Also, that method returns a BOOL of success or failure, you should be watching for that as well.
Beyond that, assuming the sqlite file is valid, there is nothing overtly wrong with the code. Can you duplicate this in a sample application? If so, you can post a link to it in your question and I will be happy to tinker around with it. If it turns out to be an Apple bug you will then have the test case ready for your radar. If it doesn't duplicate in a test then you can compare the differences to see what is wrong in the original application.