如何查看核心数据库中存储的内容?

jwk*_*knz 44 core-data ios

我正在制作一个依赖Core Data的应用程序.我能够将数据输入文本字段并存储它.

但我需要知道数据是否存储.

我正在尝试向我的tableView创建一个detailView,但我没有得到任何结果.现在我想知道的是因为我的代码做错了,或者数据是否正确存储.

如何查看应用程序CoreData数据库中存储的内容?

Zas*_*ter 35

这是我的解决方案(适用于iOS 9):

我使用automator/bash脚本在sqllitebrowser中打开数据库.该脚本在模拟器中查找最新安装的应用程序.说明:

  1. 为SQLite安装DB Browser(http://sqlitebrowser.org/)
  2. 在Apple Automator中创建新的工作流程.
  3. 拖动"运行Shell脚本块"并粘贴此代码:
cd ~/Library/Developer/CoreSimulator/Devices/
cd `ls -t | head -n 1`/data/Containers/Data/Application 
cd `ls -t | head -n 1`/Documents
open -a DB\ Browser\ for\ SQLite ./YOUR_DATABASE_NAME.sqlite
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  1. (可选)将此工作流转换为应用程序,保存并将其拖到Dock中.要刷新数据库,只需单击应用程序图标即可.

  • 在第3行第3行,我需要用`Library/Application\Support`替换`Documents`,然后它就开始了,谢谢! (6认同)

Chr*_*hen 27

如果使用sqlite作为Core Data的存储介质,则可以在模拟器中运行应用程序并尝试检查位于沙箱库文件夹中的数据库文件.

路径应类似于:〜/ Library/Application Support/iPhone Simulator/5.1/Applications/3BF8A4B3-4959-4D8F-AC12-DB8EF4C3B6E1/Library/YourAppName.sqlite

要打开sqlite文件,您需要一个工具.我使用一个名为Liya的免费工具(http://itunes.apple.com/us/app/liya/id455484422?mt=12).


Vah*_*hid 17

斯威夫特4

AppDelegate>> didFinishLaunchingWithOptions功能中添加此行:

print("Documents Directory: ", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not Found!")
Run Code Online (Sandbox Code Playgroud)

你的modelName.sqlite档案就在那里.

您可以使用免费的任何SQLite浏览器工具(如http://sqlitebrowser.org/)打开它.

  • 这对我来说效果更好:`// 使用它来检查核心数据 if let directoryLocation = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).last { print("Documents Directory: \(directoryLocation)Application支持") }` (5认同)

小智 10

存储数据库的文件夹最近已更改,而不是您希望找到它的位置.以下是我用来解决这个问题的方法:首先将此代码添加到viewDidLoad或applicationDidFinishLaunching:

    #if TARGET_IPHONE_SIMULATOR
    // where are you?
    NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
    #endif
Run Code Online (Sandbox Code Playgroud)

从这里得到这个: ios 8模拟器的文档目录在哪里

这将在运行时显示应用程序在控制台中的实际位置.然后使用SQLiteBrowser检查SQLite DB的内容.

像我的魅力一样工作;)

  • 它不在文档中,而是在库文件夹中。感谢您的回答 (2认同)

Jod*_*ins 7

如前所述,您可以使用sqllite命令行工具.

但是,您也可以设置调试标志,它将在通过核心数据执行时转储所有sql命令.

编辑方案,并在"启动时传递的参数"中添加此方案

-com.apple.CoreData.SQLDebug 1


gan*_*ogo 7

在Swift 3中,你有了NSPersistentContainer,你可以从那里检索路径,如下所示:

persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
Run Code Online (Sandbox Code Playgroud)

(假设您只使用一个持久存储)


Nik*_*Kov 6

1)获取模拟器的sql数据库的路径.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@", [paths objectAtIndex:0]);
Run Code Online (Sandbox Code Playgroud)

2)打开Finder.按Cmd+ Shift+ G.粘贴你从第1段得到的东西.例如:

/Users/USERNAME/Library/Developer/CoreSimulator/Devices/701BAC5F-2A42-49BA-B733-C39D563208D4/data/Containers/Data/Application/DCA9E9C7-5FEF-41EA-9255-6AE9A964053C/Documents
Run Code Online (Sandbox Code Playgroud)

3)在AppStore程序中下载,如SQLPro.

4)在名为"ProjectName.sqlite"的文件夹中打开文件.

做得好!你会看到这样的东西: 在此输入图像描述


Roc*_*boa 5

就macOS Sierra版本10.12.2和XCode 8而言

该文件夹应该在这里:

/Users/$username$/Library/Developer/CoreSimulator/Devices/$DeviceID$/data/Containers/Data/Application/$ApplicationID$/Library/Application Support/xyz.sqlite

要获取设备ID-打开终端并粘贴:

instruments -s devices


sta*_*Man 5

其他解决方案已经过时,或者无法轻松或快速地将您定向到SQLite文件,因此我想出了自己的解决方案,使用FileManager.SearchPathDirectory.applicationSupportDirectory该解决方案可以获取sqlite文件所在的确切路径。

func whereIsMySQLite() {
    let path = FileManager
        .default
        .urls(for: .applicationSupportDirectory, in: .userDomainMask)
        .last?
        .absoluteString
        .replacingOccurrences(of: "file://", with: "")
        .removingPercentEncoding

    print(path ?? "Not found")
}
Run Code Online (Sandbox Code Playgroud)

whereIsMySQLite() 将在此处打印您可以简单地复制粘贴到Mac上的路径:

Finder>转到>转到文件夹


Ant*_*sek 5

由于没有人指出,如何从物理设备获取sqlite DB。

以下是获取应用程序数据的方法:

在工作站上浏览我正在开发的 iOS 应用程序在设备上创建的文件?

打开 .xcappdata 文件(右键单击 > 显示包内容)后,您通常会在 AppData/Library/Application Support 文件夹中找到 DB 文件。


Ely*_*Ely 5

定位 Core Data 数据库以及查看和分析内容的一种简单便捷的方法是使用Core Data Lab等工具。

更多信息请参见: https: //betamagic.nl/products/coredatalab.html

免责声明:我是这个工具的创建者。