ios Sqlite 文件位置核心数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24133022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:44:37  来源:igfitidea点击:

Sqlite File Location Core Data

iosobjective-cswiftsqlitecore-data

提问by enlyte

Typically, the sqlite store file for core data apps is located in

通常,核心数据应用程序的 sqlite 存储文件位于

Library>Application Support>iPhone Simulator>7.1(or whichever version you are using)>Applications>(Whichever folder contains your app)>Documents

库>应用程序支持>iPhone模拟器>7.1(或您使用的任何版本)>应用程序>(包含您的应用程序的文件夹)>文档

folder, but I can't find it in IOS 8. I would assume they would just add an 8.0 folder inside the iPhone Simulator folder, but it's not there. Has anybody been able to locate it?

文件夹,但我在 IOS 8 中找不到它。我假设他们只会在 iPhone Simulator 文件夹中添加一个 8.0 文件夹,但它不在那里。有没有人能够找到它?

回答by enlyte

I managed to locate the sqlite file, and its in this path now:

我设法找到了 sqlite 文件,现在它在这个路径中:

Library/Developer/CoreSimulator/Devices/(numbers and letters)/data/Containers/Data/Application/(numbers and letters)/Documents/

Library/Developer/CoreSimulator/Devices/(数字和字母)/data/Containers/Data/Application/(数字和字母)/Documents/

(numbers and letters) stands for a folder that would be unique to your app/computer, but would look like this: 779AE2245-F8W2-57A9-8C6D-98643B1CF01A

(数字和字母)代表您的应用程序/计算机独有的文件夹,但看起来像这样:779AE2245-F8W2-57A9-8C6D-98643B1CF01A

I was able to find it by going into appDelegate.m, scrolling down to the

我可以通过进入 appDelegate.m 找到它,向下滚动到

- (NSURL *)applicationDocumentsDirectory 

method, and NSLogging the return path, like this:

方法,并 NSLogging 返回路径,如下所示:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    NSLog(@"%@",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory  inDomains:NSUserDomainMask] lastObject]);

    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
 }

This will give you your unique path, making it easier for you, because it is tricky locating it with the 2 unnamed folders/strings of letters and numbers.

这将为您提供唯一的路径,让您更轻松,因为使用 2 个未命名的文件夹/字母和数字字符串来定位它是很棘手的。

Swift 4.2:

斯威夫特 4.2:

let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
print(paths[0])

回答by hyperknot

None of the answers mentioned the simplest way to actually get the location of the DB file.

没有一个答案提到实际获取 DB 文件位置的最简单方法。

It doesn't even require you to modify your source code, as it's a simple run time switch in XCode.

它甚至不需要您修改源代码,因为它是 XCode 中的一个简单的运行时切换。

  1. Choose Edit Scheme... next to the Run button.

    edit scheme

  2. Select Run / Arguments and add the following two options:

    -com.apple.CoreData.SQLDebug 1
    -com.apple.CoreData.Logging.stderr 1
    

    option

  3. Run the app, and you'll see in the logs:

    logs

  1. 选择“运行”按钮旁边的“编辑方案...”。

    编辑方案

  2. 选择 Run / Arguments 并添加以下两个选项:

    -com.apple.CoreData.SQLDebug 1
    -com.apple.CoreData.Logging.stderr 1
    

    选项

  3. 运行应用程序,您将在日志中看到:

    日志

回答by Tunvir Rahman Tusher

Try this Simple 3 Step

试试这个简单的 3 步

  1. Copy the following code in didFinishLaunchingWithOptionsyour appdelegate.mand add a break point before the NSLog()

    (BOOL)application:(UIApplication *)application  
    
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",[paths objectAtIndex:0]);
     }
    
  1. 将以下代码复制到didFinishLaunchingWithOptions您的代码中appdelegate.m并在之前添加一个断点NSLog()

    (BOOL)application:(UIApplication *)application  
    
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@",[paths objectAtIndex:0]);
     }
    

Or in Swift:

或者在 Swift 中:

let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
print(paths[0])

enter image description here

enter image description here

2.Open Finder->Go -> Go to folder and paste the path copied from step 1

2.打开 Finder->Go -> Go to folder 并粘贴从步骤 1 复制的路径

enter image description here

enter image description here

3. Yah!!! You are in your Destination.

3. 啊!!!你在你的目的地。

回答by DiscDev

If your app is using Core Data, it's just a matter of searching for the sqlite file in Terminal:

如果您的应用程序使用的是 Core Data,那么只需在终端中搜索 sqlite 文件即可:

find ~ -name app_db_file_name.sqlite

The results will list the full path to the simulator folders containing your app.

结果将列出包含您的应用程序的模拟器文件夹的完整路径。

回答by Alexander

I hacked this one-liner (Gist): find ~/Library/Developer/CoreSimulator/Devices/ -name device.plist -exec sh -c "/usr/libexec/PlistBuddy -c \"print UDID\" '{}' | tr '\n' ' '" \; -exec sh -c "/usr/libexec/PlistBuddy -c \"print name\" '{}' | tr '\n' ' '" \; -exec sh -c "/usr/libexec/PlistBuddy -c \"print runtime\" '{}' | sed -n 's/com\.apple\.CoreSimulator.SimRuntime\.\(.*\)/\1/p'" \;

我破解了这个单行(Gist): find ~/Library/Developer/CoreSimulator/Devices/ -name device.plist -exec sh -c "/usr/libexec/PlistBuddy -c \"print UDID\" '{}' | tr '\n' ' '" \; -exec sh -c "/usr/libexec/PlistBuddy -c \"print name\" '{}' | tr '\n' ' '" \; -exec sh -c "/usr/libexec/PlistBuddy -c \"print runtime\" '{}' | sed -n 's/com\.apple\.CoreSimulator.SimRuntime\.\(.*\)/\1/p'" \;

This prints:

这打印:

14F313C8-3813-4E38-903E-2010C136B77B iPad Retina iOS-8-0
1F6B82A2-C467-479A-8CAF-074BE507AC8E iPad Air iOS-8-0
2DA83944-BE5D-4342-B330-08F67A932D8C iPad 2 iOS-7-1
48703432-A5C5-4606-9369-0D21B53EB1E8 iPhone 4s iOS-7-1
4AFB71CC-6752-4F9A-A239-4DC9CA53A8B8 iPhone 5s iOS-8-0
53A65771-DF50-4A5C-A8D2-4D1C3A5D0F60 iPhone 5 iOS-7-1
6B97C174-5914-4E89-8D17-943F0E2703BA iPhone 5 iOS-8-0
6C2DF3E9-FA26-4391-8B66-72E1467D6297 iPad 2 iOS-8-0
7F85E299-A3C2-4704-8B44-2984F6F995F5 iPad Retina iOS-7-1
8B9EDFFD-2240-476B-88AD-06ABE8F9FF51 iPhone 6 Plus iOS-8-0
97691E5D-5C38-4875-8AA7-C2B1E4ABCBB5 Resizable iPhone iOS-8-0
9BBF3408-6CA4-4CE0-BD4E-B02001F1262B iPhone 5s iOS-7-1
C9237847-F0AA-44BC-A74E-C08C2E0F7A6C Resizable iPad iOS-8-0
CC44C1BA-F39C-4410-AE34-4ABBD7806D45 iPad Air iOS-7-1
CDF4D811-5011-4464-8291-5CDA378375EA iPhone 6 iOS-8-0
E77DE00C-E244-4F25-A5E2-E6581614D5A2 iPhone 4s iOS-8-0

Update:

更新:

As someone pointed out here, it is also possible to run xcrun simctl list devicesto get a similar result:

正如有人在这里指出的那样,运行也可以xcrun simctl list devices得到类似的结果:

== Devices ==
-- iOS 7.0 --
-- iOS 7.1 --
    iPhone 4s (48703432-A5C5-4606-9369-0D21B53EB1E8) (Shutdown)
    iPhone 5 (53A65771-DF50-4A5C-A8D2-4D1C3A5D0F60) (Shutdown)
    iPhone 5s (9BBF3408-6CA4-4CE0-BD4E-B02001F1262B) (Shutdown)
    iPad 2 (2DA83944-BE5D-4342-B330-08F67A932D8C) (Shutdown)
    iPad Retina (7F85E299-A3C2-4704-8B44-2984F6F995F5) (Shutdown)
    iPad Air (CC44C1BA-F39C-4410-AE34-4ABBD7806D45) (Shutdown)
-- iOS 8.1 --
    iPhone 4s (0763EF65-DD0D-4FAD-84C7-2DE63D416526) (Shutdown)
    iPhone 5 (868ED444-8440-4115-AF37-F419CC682D2F) (Shutdown)
    iPhone 5s (E0455E5D-7315-4EC8-B05E-76284728244C) (Shutdown)
    iPhone 6 Plus (72554908-FF99-4B8F-9B87-65255ED08CFC) (Shutdown)
    iPhone 6 (32AA8133-53A4-4BE0-8CE5-0C7A87E81CAF) (Shutdown)
    iPad 2 (4AF84DE8-CBA2-47AE-A92A-0C0CEF9F41F8) (Booted)
    iPad Retina (69238B44-AAFD-4CD5-AAEA-C182D0BC300D) (Shutdown)
    iPad Air (E580AD99-0040-4EC1-B704-0C85567E6747) (Shutdown)
    Resizable iPhone (32E872FC-6513-41AB-A5B9-B23EB7D10DC8) (Shutdown)
    Resizable iPad (99997922-4A9F-4574-9E3E-EF45F893F4A2) (Shutdown)

回答by Mike Rapadas

If you're using Swift, this will return the document directory's absolute path:

如果您使用Swift,这将返回文档目录的绝对路径:

func applicationDirectoryPath() -> String {
    return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as String
}

回答by James Snook

I wrote a bash script for finding the location of your Application folder and the Application data folder within a simulator, you can download it here. Once you have unzipped the script you can navigate to the folder the script is in and use it. If you call it with no options it will give you a list of installed iOS simulators. You can then use the options to find your program. Writing:

我编写了一个 bash 脚本,用于在模拟器中查找应用程序文件夹和应用程序数据文件夹的位置,您可以在此处下载。解压缩脚本后,您可以导航到脚本所在的文件夹并使用它。如果您在没有选项的情况下调用它,它将为您提供已安装的 iOS 模拟器列表。然后您可以使用这些选项来查找您的程序。写作:

./simulatorAppFolders -s "iPad Air" -i iOS-8-0 -a <MyApp>

will find the application folder and home area folder for your app on the iPad Air iOS 8 simulator. Here's an example:

将在 iPad Air iOS 8 模拟器上找到您的应用程序的应用程序文件夹和主区域文件夹。下面是一个例子:

MacBook-Air:_posts user$ ./simulatorAppFolders -s "iPad Air" -i iOS-8-0 -a MyApp
App folder = /Users/james/Library/Developer/CoreSimulator/Devices/6A9DEE93-FAEF-4BF4-9989-BC14CD38AEA0/data/Containers/Bundle/Application/8F64CD7F-A227-43D6-98AC-41D3919827CB
dataFolder = /Users/james/Library/Developer/CoreSimulator/Devices/6A9DEE93-FAEF-4BF4-9989-BC14CD38AEA0/data/Containers/Data/Application/96C40A21-5A5C-4399-839C-B155B8A72932

Update

更新

There's an update to the original script which allows you to search based on the device type using a -d option. I've also written another script which makes a series of sensibly named folders so you can find your application and application documents. This scriptwill make a folder called iOS_SIMULATORS in your home area and you can easily navigate to your application from there.

对原始脚本进行了更新,允许您使用 -d 选项根据设备类型进行搜索。我还编写了另一个脚本,它创建了一系列命名合理的文件夹,以便您可以找到您的应用程序和应用程序文档。此脚本将在您的家庭区域创建一个名为 iOS_SIMULATORS 的文件夹,您可以从那里轻松导航到您的应用程序。

回答by vir us

I was able to locate simulators folder with the next console output:

我能够使用下一个控制台输出找到模拟器文件夹:

NSLog(@"app dir: %@",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);

in my case it was (Xcode 6 beta)

在我的情况下是(Xcode 6 beta)

app dir: file:///Users/user/Library/Developer/CoreSimulator/Devices/D00C56D4-DDA3-47B0-9951-27D3B9EC68E8/data/Applications/6ABF3E54-37F5-4AD1-A223-F7467F601FB6/Documents/

回答by Jochen Bedersdorfer

Based on Michaels answers, this is for Swift 3

根据 Michaels 的回答,这是针对 Swift 3 的

    func applicationDirectoryPath() -> String {
        return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String
    }

回答by Nazir

After new updates of Xcode I found that the sql files are now stored in new folder /Library/Application Supportthis could be a unique situation but if you did not found the sql files in document folder search them here:

在 Xcode 的新更新后,我发现 sql 文件现在存储在新文件夹中,/Library/Application Support这可能是一种独特的情况,但如果您没有在文档文件夹中找到 sql 文件,请在此处搜索它们:

~/Library/Developer/CoreSimulator/Devices/[@device_id@]/data/Containers/Data/Application/[@application_id@]/Library/Application Support/

~/Library/Developer/CoreSimulator/Devices/[@device_id@]/data/Containers/Data/Application/[@application_id@]/Library/Application Support/

before it was stored in Documentsfolder:

在它被存储在Documents文件夹之前:

~/Library/Developer/CoreSimulator/Devices/[@device_id@]/data/Containers/Data/Application/[@application_id@]/Documents/

~/Library/Developer/CoreSimulator/Devices/[@device_id@]/data/Containers/Data/Application/[@application_id@]/Documents/