xCode Sqlite数据库创建没有Z_METADATA [英] xCode Sqlite Database Creation Without Z_METADATA

查看:130
本文介绍了xCode Sqlite数据库创建没有Z_METADATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的sqlite数据库使用核心数据模型创建时,在这一行:

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

 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.

注意:

如果我删除了应用程序,重建和安装在我的设备 - .sqlite文件日期两天前,.mom文件日期昨天。如果有必要,数据库是否在编译时重新创建?我的项目中没有.sqlite文件,只有.xcdatamodel。

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.

感谢您的时间。

- (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


推荐答案

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

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

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

此外,该方法返回一个成功或失败的BOOL,

Also, that method returns a BOOL of success or failure, you should be watching for that as well.

除此之外,假设sqlite文件是有效的,代码没有明显错误。您可以在示例应用程序中复制此内容吗?如果是这样,你可以在你的问题中发布一个链接,我会很乐意与它纠缠。如果原来是一个苹果的bug,你会有测试用例准备好你的雷达。如果它在测试中不重复,那么你可以比较差异,看看原始应用程序有什么问题。

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.

这篇关于xCode Sqlite数据库创建没有Z_METADATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆