没有 Z_METADATA 的 xCode Sqlite 数据库创建

发布于 2024-09-15 12:03:35 字数 2234 浏览 1 评论 0原文

当我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚你发端 2024-09-22 12:03:35

请从这个基本的完整性检查开始:将数据库带到您的 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).

心房的律动 2024-09-22 12:03:35

Core Data 在打开存储时似乎首先查找元数据,因此有关 Z_METADATA 的投诉通常表明文件已损坏或格式错误。我怀疑复制的文件有问题(权限、损坏等)或商店的选项有问题。

我建议:

  1. 在应用程序包中以只读方式打开商店,即不复制它,看看它是否可以正常打开。如果是这样,那么问题就出在复制上。
  2. 向复制方法提供错误并记录其返回。副本可能会微妙地失败。
  3. 获取复制文件的大小。该文件可能最终位于目录中但为空。
  4. 传递一个 nil 选项字典以确保这不是问题。

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:

  1. Open the store as readonly in the app bundle i.e. without copying it and see if it opens fine. If it does, then the problem is in the copying.
  2. Provide an error to the copy method and log its return. The copy might be subtly failing.
  3. Get the size of the copied file. The file might end up in the directory but empty.
  4. Pass a nil options dict to make sure that is not the problem.
白云悠悠 2024-09-22 12:03:35

首先,您需要捕获这些错误并至少报告它们:

[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];

此外,该方法返回成功或失败的 BOOL,您也应该注意这一点。

除此之外,假设 sqlite 文件有效,则代码没有任何明显的错误。您可以在示例应用程序中复制此内容吗?如果是这样,您可以在问题中发布指向它的链接,我很乐意修改它。如果事实证明这是 Apple 的错误,那么您将为您的雷达准备好测试用例。如果它在测试中没有重复,那么您可以比较差异以查看原始应用程序中存在什么问题。

First, you need to catch these errors and at least report them:

[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文