xCode Sqlite Database Creation Without Z_METADATA

时光毁灭记忆、已成空白 提交于 2019-12-07 12:18:14

问题


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

回答1:


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).




回答2:


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.



回答3:


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.



来源:https://stackoverflow.com/questions/3533874/xcode-sqlite-database-creation-without-z-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!