XCode4 - 如何查找Core Data创建的SQLite文件?

7
我刚开始使用Core Data。我想设置一个预填充的数据库。我在某个地方读到,当运行Core Data应用时,它会创建一个SQLite文件。但我不知道在哪里查找它。
我按照这个博客上的说明操作,但是没有在指定目录/Users/<Username>/Library/Application Support/iPhone Simulator/User/Application/<Application GUID>/Documents/<database name.sqlite>或应用程序目录中找到SQLite文件。
这是我的持久化协调器代码。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSString *storePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"coredata.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"coredata" 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;
}
4个回答

16

对于我来说,那些文件在~/Library/Application Support/iPhone Simulator/[SDK版本]/Applications/[应用程序GUID]/Documents中,无论是Xcode 3还是Xcode 4。

如果您有多个SDK,请确保在所有不同的SDK版本目录中查找您的应用程序。


如何获取应用程序 GUID? - Pierre de LESPINAY

1
对我来说,它在whatMuregSaid / [应用程序指南] / Library / Application Support / [AppName] / filename.sqlite中。

1
请按照以下步骤操作:
1. 在AppDelegate.swift的applicationDocumentsDirectory函数中添加以下代码: let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) //在控制台中查找此内容,将打印出sqlite数据库的位置,您可以使用SQLite查看器打开它 print("urls.last \(urls.last)") 2. 在控制台中查找此内容,将打印出sqlite数据库的位置,在终端中输入open whatever_the_location_path_was以打开该位置。
3. 下载SQLite查看器http://sqlitebrowser.org/
4. 在文件夹中右键单击yourapp.sqlite文件,选择您下载的sqlitebrowser打开。
祝好运!

0

希望这能对你有所帮助

  1. /Users/Username/ 然后按下 cmd+shift+G 并输入 /Users/Username/Library,现在你会看到 Library 文件夹,在它后面进入 Application Support/iPhone Simulator/7.1(或 7.1-64)/Applications/F84D4CC8-326E-4A2E-8A37-F1A755D6FCC4/Documents,你会看到三个文件,其中一个是 .sqlite 文件。

  2. 要查看此 sqlite 文件的结构和其他信息,最有效的方法是使用 SQLITE MANAGER(firefox 的附加组件 https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/),添加它,然后单击 TOOL。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接