Xcode Create Sqlite Database Without Z_METADATA

When my sqlite database is created using the underlying data model, on this line:

if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 

I get an error message:

  NSUnderlyingException=I/O SQLite error code:1, 'no such table: Z_METADATA' 

Any idea how to fix this? I tried for many days. The database is created and copied to the document directory on my device.

Note:

If I uninstall the application, rebuild and install it 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 do not have a .sqlite file in my project, just a .xcdatamodel.

Thank you 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 
+3
source share
3 answers

Please start with this basic health check: bring the database to your Mac, open it with sqlite3.exe and see if the table is present.

If so, your program points to the wrong database. If this is not the case, your table has never been created (or the database is corrupted like hell, but that would be amazing).

0
source

The master data seems to look for metadata first when they open the repository, so a complaint about Z_METADATA usually indicates a damaged file or a file in the wrong format. I would suspect either a problem with the copied file (resolution, damage, etc.), Or a problem with options for the store.

I would suggest:

  • Open the repository as soon as you read in the application bundle, that is, without copying it and check whether it opens normally. If so, then the problem is copying.
  • Provide an error to the copy method and record its return. A copy may be slightly unsuccessful.
  • Get the size of the copied file. The file may appear in the directory, but empty.
  • Pass nil parameters to make sure this is not a problem.
0
source

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

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

In addition, this method returns BOOL success or failure, you should also keep track of this.

Also, assuming the sqlite file is valid, there is nothing explicit in the code. Can you duplicate this in an example application? If so, you can post a link to it in your question, and I will be happy to work with him. If this turns out to be Apple's error, you will have a ready-made test case for your radar. If it is not duplicated in the test, you can compare the differences to find out what is wrong in the original application.

0
source

Source: https://habr.com/ru/post/885583/


All Articles