【问题标题】:sqlite file does not fill the tableView - it works with simple data though - ? (sqlite, core data, pre-populate data)sqlite 文件不填充 tableView - 虽然它适用于简单数据 - ? (sqlite、核心数据、预填充数据)
【发布时间】:2011-12-17 23:55:50
【问题描述】:

我有一个预先填充好的 sqlite 文件,我将它复制到我的项目中(在文件夹中,然后到 xcode 项目窗口中)

当我使用终端检查 sqlite 文件时,它工作正常,我的 sqlite 文件中包含正确的数据。

然后我尝试用 sqlite 文件中的数据填充我的 tableView,但 tableView 仍然是空的。

你能告诉我我应该看哪里吗?

我首先尝试了一些数据并且它可以工作(在 applicationDidFinish 中注释了什么...),但是对于我的 sqlite 文件,它不起作用:

这是我的代码:(这是教程网址:http://www.raywenderlich.com/980/core-data-tutorial-how-to-preloadimport-existing-data

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    NSManagedObjectContext *context = [self managedObjectContext];

    /*
    THIS WORKS :
    FailedBankInfo *failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo" 
                                                                   inManagedObjectContext:context];
    failedBankInfo.name = @"the name";
    failedBankInfo.city = @"the city";
    failedBankInfo.state = @"the state";

    FailedBankDetails *failedBankDetails = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankDetails"
                                                                         inManagedObjectContext:context];

    failedBankDetails.closeDate = [NSDate date];
    failedBankDetails.updatedDate = [NSDate date];
    failedBankDetails.zip = [NSNumber numberWithInt:12345];

    failedBankInfo.details = failedBankDetails;
    failedBankDetails.info = failedBankInfo;

    NSError *error;
    if (![context save:&error]){
        NSLog(@"%@,", [error localizedDescription]);
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" 
                                              inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (FailedBankInfo *info in fetchedObjects){
        NSLog(@"name : %@", info.name);
        FailedBankDetails *details = info.details;
        NSLog(@"zip : %@", details.zip);
    }
    [fetchRequest release];*/

    FailedBanksListViewController *root = (FailedBanksListViewController *)[_navController topViewController];
    root.context = [self managedObjectContext];
    [window addSubview:_navController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

/** Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"FailedBanksCD.sqlite"];
    /*NSString *storePath = [[self applicationDocumentsDirectory] 
                           stringByAppendingPathComponent: @"FailedBanksCD.sqlite"];
    */
     //NSURL *storeURL = [NSURL fileURLWithPath:storePath];

    // Put down default db if it doesn't already exist
    NSString *storePath = [NSString stringWithFormat:@"%@", storeURL];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] 
                                      pathForResource:@"FailedBanksCD" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }
    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

非常感谢

【问题讨论】:

    标签: objective-c core-data sqlite persistent-storage


    【解决方案1】:

    在我看来,数据库似乎没有复制到 doc-dir。你能移动这部分吗

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }
    

    在这个 if 块之外:

    if (![fileManager fileExistsAtPath:storePath]) {
        ...
    }
    

    您必须在重试之前从 doc-dir 中删除当前的空 DB(在模拟器中删除,从设备中删除应用程序并重新安装)。

    【讨论】:

    • 感谢 ott--不幸的是,这不会刷新表格视图。如果我使用select * from ZFAILEDBANKINFO limit 3 检查包含从doc-dir 复制的sqlite 的内容,它不会在终端中显示任何内容,这可能是因为它已加密。有机会,你能看看我的代码吗?我已将包含 sqlite 和 xcode 项目的 zip 文件放在:www.pondb.com(我的个人网站)。
    • 您是否在使用错误的数据库?您尝试提取的数据在 banklist.sqlite3 中,而您复制了数据库 FailedBanksCD.sqlite。
    • 感谢 ott--,我想我在复制 failedbank sqlite 文件时弄混了,一定是因为我一次又一次地尝试,终于成功了。感谢您的帮助,干杯
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2011-04-01
    • 1970-01-01
    • 2011-10-22
    • 2011-09-29
    • 2011-12-02
    相关资源
    最近更新 更多