ios 如何在 iPhone 上访问 SQLite 数据库实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/159889/
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-30 15:18:25  来源:igfitidea点击:

How do I access SQLite database instance on iPhone?

iosiphonesqlite

提问by Keith Fitzgerald

I'm developing an iPhone app that uses the built-in SQLite database. I'm trying to view and open the database via the sqlite3command line tool so I can execute arbitrary SQL against it.

我正在开发一个使用内置 SQLite 数据库的 iPhone 应用程序。我正在尝试通过sqlite3命令行工具查看和打开数据库,以便我可以对其执行任意 SQL。

When I run my app in the simulator, the .sqlitefile it creates is located at ~/Library/Application Support/iPhone Simulator/User/Applications/.

当我在模拟器中运行我的应用程序时,.sqlite它创建的文件位于~/Library/Application Support/iPhone Simulator/User/Applications/.

How can I see that file on the physical iPhone?

如何在物理 iPhone 上查看该文件?

回答by Tr0yJ

Instructions for Xcode 6.0.1

Xcode 6.0.1 说明

  1. Xcode > Open > YourProject
  2. Xcode > Product > Run
  3. Xcode > Window > Devices
  4. (Column 1 - select) Devices > YourDeviceName
  5. (Column 2 - select) Installed Apps > YourAppName
  6. (Column 2 - select) Cog under 'Installed Apps' list
  7. (Pop-Up - select) Download Container...
  8. Save to location
  9. Right click on 'YourAppName.xcappdata'
  10. Select 'Show Package Contents'
  11. AppData > Documents > YourDatabase.sqlite
  1. Xcode > 打开 > 你的项目
  2. Xcode > 产品 > 运行
  3. Xcode > 窗口 > 设备
  4. (第 1 列 - 选择)设备 > YourDeviceName
  5. (第 2 列 - 选择)已安装的应用程序 > YourAppName
  6. (第 2 列 - 选择)“已安装的应用程序”列表下的 Cog
  7. (弹出 - 选择)下载容器...
  8. 保存到位置
  9. 右键单击“YourAppName.xcappdata”
  10. 选择“显示包裹内容”
  11. AppData > 文档 > YourDatabase.sqlite

enter image description here

在此处输入图片说明

回答by Lounges

In Xcode select window->organizer and expand the node next to your application in the applications section on your phone. Select the black downward pointing arrow next to application data and save the file anywhere on your desktop. Your sqlite database should be in there somewhere.

在 Xcode 中,选择 window->organizer 并展开手机应用程序部分中应用程序旁边的节点。选择应用程序数据旁边的黑色向下箭头并将文件保存在桌面上的任意位置。您的 sqlite 数据库应该在某个地方。

As for how to go about getting it back on the phone once your done i have no clue.

至于如何在完成后将其取回电话,我不知道。

回答by Echilon

In XCode 4, you do the same as Lounges suggested, which will save the whole file structure for your app to your destination of choice. Rename the .xcappdata file which is saved to .sqlite so you can open it by double clicking, then you can find the file from the device.

在 XCode 4 中,您按照 Lounges 的建议执行相同的操作,这会将您的应用程序的整个文件结构保存到您选择的目的地。将保存为 .sqlite 的 .xcappdata 文件重命名,这样您就可以双击打开它,然后您就可以从设备中找到该文件。

回答by abbood

This one works if you jailbreak your iPhone.. I don't know why anyone would have any issues with jailbreaking their phone as I've been using it for development for quite some time and found no problems, also it is not uncommon for sqlite to perform differentlyon the device vs simulator:

如果你越狱了你的 iPhone,这个就可以工作..我不知道为什么有人会在越狱他们的手机时遇到任何问题,因为我已经使用它进行开发一段时间了并且没有发现任何问题,这对于 sqlite 也并不少见以不同地执行所述设备模拟器VS上:

  1. Jail break your phone (there tutorials all over the web)
  2. Set your cydia user level to developer
  3. Install sqlite3 into your phone: go to cydia > manage > sources > cydia/telesphoreo > sqlite3
  4. ssh into your phone using iphone tunnelroot ssh password: "alpine"
  5. Type which sqlite3to ensure you have it installed
  6. browse to the location of your db.. a breakpoint in your code should tell you where it is located.. in my code it looks something like this

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex: 0]; 
    NSString *pathName = [documentsDirectory stringByAppendingPathComponent:filename];
    return pathName;
    

    Notice that if you run this on the simulator.. you'll get a location like the following: /Users/admin/Library/Application Support/iPhone Simulator/6.0/Applications/42302574-7722-48C1-BE00-91800443DA7C/Documents/email-524200.edb

  1. 监狱打破你的手机(网上到处都是教程)
  2. 将你的 cydia 用户级别设置为developer
  3. 将 sqlite3 安装到您的手机中:转到 cydia > manage > sources > cydia/telesphoreo > sqlite3
  4. 使用iphone 隧道root ssh 密码ssh 进入您的手机:“alpine”
  5. 键入which sqlite3以确保已安装
  6. 浏览到您的数据库的位置.. 代码中的断点应该告诉您它的位置.. 在我的代码中它看起来像这样

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex: 0]; 
    NSString *pathName = [documentsDirectory stringByAppendingPathComponent:filename];
    return pathName;
    

    请注意,如果您在模拟器上运行它.. 您将获得如下所示的位置: /Users/admin/Library/Application Support/iPhone Simulator/6.0/Applications/42302574-7722-48C1-BE00-91800443DA7C/Documents/email-524200.edb

On the device it will look like this: /var/mobile/Applications/FB73857F-A822-497D-A4B8-FBFB269A8699/Documents/email-523600.edb

在设备上,它将如下所示: /var/mobile/Applications/FB73857F-A822-497D-A4B8-FBFB269A8699/Documents/email-523600.edb

Then just type sqlite3 %dbname%and you can execute sql statements right on your phone.. without copying it over or whatever.

然后只需键入sqlite3 %dbname%,您就可以直接在手机上执行 sql 语句......无需复制它或其他任何东西。

回答by millenomi

Exactly in the same way you do on the simulator. There are very few (important) differences between the device and simulator, and file access and library loading are for the most part not part of them.

与您在模拟器上所做的完全相同。设备和模拟器之间几乎没有(重要的)区别,文件访问和库加载在很大程度上不是它们的一部分。

回答by millenomi

Your question remains a little vague. "See" in what sense? Do you create the SQLite database? How? Have you placed it manually in the Simulator's filesystem area? Are you perhaps asking how to do that on the iPhone?

你的问题还是有点含糊。“见”是什么意思?您是否创建了 SQLite 数据库?如何?您是否手动将其放置在模拟器的文件系统区域中?你可能会问如何在 iPhone 上做到这一点?

The easiest way is to precreate an empty database with the sqlite3 command-line tool, have it as a resource in your application, then copy it in your application sandbox's documents folder. You can get the path to your resources folder via NSBundle's pathForResource:ofType: method, then grab the path to your Documents folder via NSSearchPathForDirectoriesInDomains() for the NSDocumentsDirectory folder in the NSUserDomainMask, then copy the file via NSFileManager's methods.

最简单的方法是使用 sqlite3 命令行工具预先创建一个空数据库,将其作为应用程序中的资源,然后将其复制到应用程序沙箱的文档文件夹中。您可以通过 NSBundle 的 pathForResource:ofType: 方法获取资源文件夹的路径,然后通过 NSUserDomainMask 中 NSDocumentsDirectory 文件夹的 NSSearchPathForDirectoriesInDomains() 获取 Documents 文件夹的路径,然后通过 NSFileManager 的方法复制文件。

Otherwise, you can use SQLite's functions to create a new database from scratch by supplying appropriate SQL commands to define its schema.

否则,您可以使用 SQLite 的函数通过提供适当的 SQL 命令来定义其架构来从头开始创建新数据库。

回答by Clement Ho

The Easiest way to do it by far is using iExplorer to download the file from your app. and then use SQLite Professional read-onlyto read the file. Even thought it is not realtime but at least it is free. :-)

到目前为止,最简单的方法是使用 iExplorer 从您的应用程序下载文件。然后使用SQLite Professional 只读读取文件。甚至认为它不是实时的,但至少它是免费的。:-)

回答by user3349763

The Download Container method is the one I found to be the best. However, you have to be careful that some times, if you try to e-mail it or attach it from within the app then the file that is sent out would be empty.

下载容器方法是我认为最好的方法。但是,您必须小心,有时,如果您尝试通过电子邮件发送它或从应用程序中附加它,那么发送的文件将是空的。