在设备上调试sqlite数据库
我目前正在开发 Android 的 WiFi 应用程序。我在尝试访问设备上的数据库时遇到问题。在模拟器中调试对我来说不起作用,因为模拟器中不支持 WiFi。我尝试使用
adb pull data/data/package-name/databases/database-name
但我收到错误“权限被拒绝”将数据库文件从设备中拉出。 在此答案中 Android:数据库文件存储在哪里?, Commonsware 建议通过在调试模式下运行来拉取数据库文件。但它也不起作用。任何关于如何在不root设备的情况下调试数据库的帮助将不胜感激。
I am presently working on an WiFi application for Android. I am having trouble trying to access the database on the device. Debugging in the emulator doesn't work for me, because there is no WiFi support in the emulator. I tried pulling the database file out of the device by using
adb pull data/data/package-name/databases/database-name
But I get the error "Permission denied.".
In this answer Android: Where are database files stored?, Commonsware has suggested to pull database file by running in debug mode. But it doesn't work too. Any help on how to debug the database without rooting the device would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我将重复一下另一个答案:
将文件复制到设备上的外部存储
因此,如果您希望从设备中提取应用程序数据库,您可以从 Android Studio 启动应用程序的调试版本,使用
adb shell
连接并运行以下命令:或者您可以使用使用相对路径的较短版本,并通过跳过对主目录的引用
~
也可以省略sh -c
部分:这将复制您的
db-file.db
到 SD 卡的根目录 /外部存储。现在,您可以使用文件管理器、adb pull
或任何您喜欢的其他方式轻松地从那里获取它。请注意,使用这种方法,您的应用不需要拥有 WRITE_EXTERNAL_STORAGE 权限,因为复制是由始终可以写入外部存储的shell
用户完成的。直接将文件复制到计算机
您还可以使用以下命令将数据库直接复制到计算机:
由于
CR/LF<,上面的
adb shell
命令在 Windows 主机上无法正常工作/code> 符号转换,除非您从bash
shell 运行它或使用未记录的adb exec-out
命令(一些评论提到他们仍然无法让它正常工作)在 Windows 计算机上,因此您的情况可能会有所不同):写入文件返回设备
如果您修改了计算机上的文件并希望将修改写回设备,请使用以下命令:
或者如果您更喜欢
adb shell
版本而不是未记录的exec-in
,使用以下命令(但请阅读上面有关 WindowsCR/LF
转换的问题):状态更新
截至 2023 年 7 月,上述所有命令仍然有效
I'll repeat myself from another answer:
Copy files to external storage on the device
So if you wish to pull your application database from the device you can launch a debug build of the application from Android Studio, connect with
adb shell
and run the following command:or you can use a shorter version which uses relative paths and by skipping the reference to the home directory
~
can omit thesh -c
part too:This will copy your
db-file.db
to the root of your SD card / external storage. Now you can easily get it from there by using file manager,adb pull
or whatever else you like. Note that with this approach, there is NO need for your app to haveWRITE_EXTERNAL_STORAGE
permission, as the copying is done by theshell
user who can always write to the external storage.Copy files directly to the computer
You can also copy a database directly to your computer with the following command:
The
adb shell
command above will not work correctly on Windows host because of theCR/LF
symbols conversion, unless you run it from abash
shell or use the undocumentedadb exec-out
command instead (some comments mention they still cannot get it to work correctly on a Windows machine, so your mileage may vary):Write files back to the device
If you modified the file on your computer and wish to write the modifications back to the device use the following command:
or if you prefer an
adb shell
version over undocumentedexec-in
, use the following (but read the concern about the WindowsCR/LF
conversion above):Status update
All the commands above are still working as of July, 2023
我在我的 MAC 上使用这个 shell 脚本,它将数据库直接复制到我的主文件夹。简单的一键解决方案,只需更改包名称 (com.example.app) 和数据库名称 (database.sqlite)
简单脚本脚本
接受参数 [package_name] [database] 的
I use this shell script on my MAC, that copies database directly to my home folder. Easy one click solution, just change package name (com.example.app) and database name (database.sqlite)
Simple Script
Script which accepts arguments [package_name] [database]
查看和管理 Android 应用程序数据库的最佳方法是使用此库 https://github.com/sanathp/DatabaseManager_For_Android
使用此库,您可以从应用程序本身管理应用程序 SQLite 数据库。
您可以查看应用程序数据库中的表,更新、删除、向表中插入行。应用程序中的一切。
它是一个单一的java活动文件,只需将java文件添加到您的源文件夹中。开发完成后,从您的src文件夹中删除java文件即可。
它对我帮助很大。希望它也对你有帮助。
您可以在此处观看 1 分钟演示:http://youtu.be/P5vpaGoBlBY
The best way to view and manage you android app database is to use this library https://github.com/sanathp/DatabaseManager_For_Android
With this library you can manage your app SQLite database from you app itself.
you can view the tables in your app database , update ,delete, insert rows to your tables .Everything from your app.
Its a single java activity file ,just add the java file to your source folder.When the development is done remove the java file from your src folder thats it .
It helped me a lot .Hope it helps you too .
You can view the 1 minute demo here : http://youtu.be/P5vpaGoBlBY
虽然这是一个老问题,但我认为它仍然相关并且值得当前状态的答案。有一些可用的工具允许您直接检查数据库(无需从设备或模拟器中提取它们)。
我最近发现(也是最喜欢)的工具是 Android 调试数据库。
您只需要添加此依赖项:
不需要更多代码。
启动应用程序后,打开 logcat 并过滤“DebugDB”,您会发现一条消息,说明
它适用于每个浏览器,您可以检查数据库表和共享首选项。
它也适用于默认模拟器和 Genymotion 模拟器。
我之前使用的工具是stetho。
缺点:你需要添加一些代码并且绑定到 Chrome 浏览器。
优点:您还可以选择检查网络流量。
Although, it's an old question I think it's still relevant and deserves a current state answer. There are tools available, which allow you to inspect databases directly (without the need to pull them from the device or emulator).
The tool, I most recently discovered (and favor most) is Android Debug Database.
You only need to add this dependency:
No further code is required.
After you started your app, open logcat and filter for "DebugDB" and you will find a message saying
It works with every browser and you can inspect your database tables and shared preferences.
It also works with the default and the Genymotion emulators.
The tool I used before is stetho.
Downside: You need to add a bit of code and you are bound to the Chrome browser.
Advantage: You have the option to also inspect network traffic.
在新的 Android Studio 4.1 中,有新的 数据库检查员。
您可以选择以下内容菜单栏的选项
View >工具窗口> Database Inspector
打开它(Android Studio 4.2 中的App Inspector
)。更详细的说明可以在此博客和探索 Android Studio 中的数据库检查器 中等文章。另一种方法是使用 stetho 来实现此目的。您添加依赖项,然后可以在设备插入时使用 Chrome DevTools ( chrome://inspect ) 检查数据库。
In the new Android Studio 4.1 there is the new Database Inspector.
You can select the following options from the menu bar
View > Tool Windows > Database Inspector
to open it (App Inspector
in Android Studio 4.2). More detailed instructions can be found in this blog and in Exploring the Database Inspector in Android Studio medium article.Another way is to use stetho for this. You add the dependency and then can use the Chrome DevTools ( chrome://inspect ) to check the database when the device is plugged in.
在我的应用程序中,我将数据库导出到 SD 卡。一旦数据库位于 SD 卡上,就可以通过将设备插入计算机来访问它。
看这篇文章:在 Android 上将数据库备份到 SDCard
In my application I export the database to the SD card. Once the database is on the SD card it can be accessed by plugging the device into your computer.
Look at this post: Making a database backup to SDCard on Android
如果你尝试
注意
双引号!
If you get
try
Note the double quote!
我只是这样做了:
然后我可以调试 sqlite 数据库或使用正确的权限从 shell 执行任何我想做的事情。
I simply did:
Then I can debug an sqlite database or whatever I wanna do from shell with the right permissions.
Android Studio 4.1 添加了查看/编辑 Android SQLite 数据库的新功能。
如何打开Database Inspector
要在Android Studio 中打开Database Inspector,您需要选择
View >工具窗口>菜单栏中的数据库检查器
。此外,您还需要在运行 API 级别 26 或更高级别的设备上运行应用程序。
使用此工具,您可以
查询您的数据库
修改和调试您的数据库
Android Studio 4.1 Added a new feature to view/ edit Android SQLite databases.
How to open Database Inspector
To open the Database Inspector in Android Studio, you need to select
View > Tool Windows > Database Inspector
from the menu bar.Also you need to run the app to a device running API level 26 or higher.
Using this tool you can
Query your databases
Modify and debug your database
如果 apk 可调试,则有一种方法可以使用(非 root)adb shell 中名为 run-as 的程序来复制应用程序的私有文件。
There is a way if an apk is debuggable to use a program called run-as from the (non-root) adb shell to copy an application's private file.
这是分步说明 - 大部分取自其他答案的组合。这适用于未解锁的设备。
连接您的设备并在调试模式下启动应用程序。
将数据库文件从应用程序文件夹复制到 SD 卡:执行:
./adb -d shell 'run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite'
将数据库文件拉到您的计算机上:执行:
./adb pull /sdcard/ 执行:./adb
安装 Firefox SQLLite 管理器:< a href="https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/" rel="nofollow">https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
打开 Firefox SQLLite Manager 并打开上面第 3 步中的数据库文件。< /p>
享受吧!
Here is step by step instructions - mostly taken from a combination of the other answers. This works with devices that are not unlocked.
Connect your device and launch the application in debug mode.
Copy the database file from your application folder to your sd card: execute:
./adb -d shell 'run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite'
Pull the database files to your machine: execute:
./adb pull /sdcard/ execute: ./adb
Install Firefox SQLLite Manager: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Open Firefox SQLLite Manager and open your database file from step 3 above.
Enjoy!
您需要以 root 身份运行 adb,或者在 root 手机上使用它。
要以 root 身份运行 adb,请使用
adb root
另请参阅:为什么使用 adb 时数据文件夹的访问被拒绝?
You need to be running adb as root, or be using it on a rooted phone.
To run adb as root, use
adb root
See also: Why do I get access denied to data folder when using adb?
在 Android 4.4.2 上,
run-as
-and-cat-to-sdcard 解决方案都不适合我。我不确定,但我怀疑这可能是由于run-as
工具无法正确处理新的sdcard_r
和sdcard_rw
权限。我首先必须将数据库文件复制到应用程序私有内部存储中的
/files
:然后复制到
/sdcard/Android/data/com.example.myapp/files
在 Javaland 中(这需要 WRITE_EXTERNAL_STORAGE 权限):最后,我将文件复制到我的笔记本电脑上:
None of the
run-as
-and-cat-to-sdcard solutions worked for me on Android 4.4.2. I'm not sure, but I suspect it may be due to therun-as
tool not correctly handling the newsdcard_r
andsdcard_rw
permissions.I first had to copy the database file to
/files
in my application's private internal storage:Then I copied to
/sdcard/Android/data/com.example.myapp/files
in Javaland (this requires theWRITE_EXTERNAL_STORAGE
permission):Finally, I copied the file to my laptop:
我的设备没有 SD 卡
so the first solution did not work for me.
如果您遇到类似问题,请尝试这样:
My Device was not having sdcard
so the first solution did not work for me.
If you are having similar issue try like this:
试试这个应用程序:SQLiteWeb (https://play.google .com/store/apps/details?id=br.com.cm.sqliteweb)。它提供对数据库的远程访问,而无需将其拉出。
在付费版本中,它具有私有数据库的根访问权限(/data/data/package-name...)或实现内容提供程序以使用 SQLiteWeb 连接(应用程序内的说明)
但如果想保留免费版本,您可以在外部存储中创建数据库:
Try this app: SQLiteWeb (https://play.google.com/store/apps/details?id=br.com.cm.sqliteweb). It provides remote access to your database without pulling it out.
In paid version, it has root access for private database (/data/data/package-name...) or implement a Content Provider to connect using SQLiteWeb (Instructions inside app)
But if want to stay with the free version, you may create your database in external storage:
在 OSX 上,使用 @Tadas 答案以及 automator 和 sqllitebrowser(https://github.com/sqlitebrowser/sqlitebrowser ):
打开 Automator 并创建新工作流程。
添加“运行 Shell 脚本”操作。
粘贴此:
源~/.bash_profile
adb shell 'run-as cat /data/data/your_app_uid/databases/db.name > /tmp/db.name'
adb pull /tmp/db.name ~/
open -a sqlitebrowser ~/db.name
单击运行以刷新 sqlitebrowser 上的数据库。
On OSX,using @Tadas answer with automator and sqllitebrowser(https://github.com/sqlitebrowser/sqlitebrowser):
open Automator and create new workflow.
add "Run Shell Script" action.
Paste this :
source ~/.bash_profile
adb shell 'run-as cat /data/data/your_app_uid/databases/db.name > /tmp/db.name'
adb pull /tmp/db.name ~/
open -a sqlitebrowser ~/db.name
click run to refresh the database on sqlitebrowser.
cd
(对于 Windows 上的我来说cd C:\Users\Willi\AppData\Local\Android\sdk
)cd platform- tools
adb shell
(仅当只有一个模拟器运行时才有效)cd data/data
su
(获得超级用户权限)sqlite3
;
终止它们,否则语句不会发出,而是换行。)注意:如果需要列出目录内容,请使用
ls
(Linux) 或dir
(Windows)。cd <ANDROID_SDK_PATH>
(for me on Windowscd C:\Users\Willi\AppData\Local\Android\sdk
)cd platform-tools
adb shell
(this works only if only one emulator is running)cd data/data
su
(gain super user privileges)cd <PACKAGE_NAME>/databases
sqlite3 <DB_NAME>
;
, otherwise the statement is not issued and it breaks to a new line instead.)Note: Use
ls
(Linux) ordir
(Windows) if you need to list directory contents.