代码之家  ›  专栏  ›  技术社区  ›  Bryan

xcode sqlite数据库创建,无z_元数据

  •  3
  • Bryan  · 技术社区  · 14 年前

    当使用核心数据模型创建我的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
    
    3 回复  |  直到 14 年前
        1
  •  0
  •   MPelletier    14 年前

    请从这个基本的健全性检查开始:将数据库带到您的Mac,用sqlite3.exe打开它,然后查看表是否存在。

    如果是,那么您的程序指向错误的数据库。 如果不是这样,那么就永远不会创建表(或者数据库像地狱一样被破坏,但这会令人惊讶)。

        2
  •  0
  •   TechZen    14 年前

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

    我建议:

    1. 在应用程序包中以只读方式打开存储区,即不复制存储区,查看它是否打开良好。如果是这样,那么问题就在于复制。
    2. 为copy方法提供一个错误并记录其返回。拷贝可能有点失败。
    3. 获取复制文件的大小。文件可能会在目录中结束,但为空。
    4. 通过A nil 选项dict以确保不是问题所在。
        3
  •  0
  •   Marcus S. Zarra    14 年前

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

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

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

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