自应用发布以来,我一直在使用Android Studio开发应用。

直到最近,一切都很好,我必须调试和检查数据库文件。由于我不知道如何直接看到数据库,当我调试生成数据库文件时,我不得不将数据库文件从我的手机导出到PC上。

为此,我必须打开DDMS >文件资源管理器。一旦我打开DDMS,我必须重新连接USB,我失去了调试线程。在检查数据库文件之后,我必须关闭DDMS并重新连接USB以回到调试模式。

这太复杂了。有人有更好的方法来做到这一点在Android Studio(我知道它更容易在Eclipse) ?


当前回答

我想知道为什么这里没有提到这个解决方案!

...在Android Studio 4.1及更高版本中…

1.在运行API级别26或更高的模拟器或连接设备上运行应用程序。

2.在菜单栏中选择“View > Tool Windows > Database Inspector”。

3.从下拉菜单中选择正在运行的app进程。

4.当前运行的应用程序中的数据库显示在“数据库”窗格中。展开要检查的数据库的节点。

其他回答

最简单的方法是连接你的设备并运行Android Studio 然后从工具栏:

视图——>工具窗口——>设备文件资源管理器 进入data/数据并找到您的包 找到你想要浏览并下载的DB文件 我推荐使用这个在线工具:https://sqliteonline.com/来查看db文件

另一种方法是使用Stetho库:

在build.gradle中添加Stello依赖: 编译com.facebook.stetho: stetho: 1.5.0的 在你的应用程序类或主活动的onCreate()上放以下一行: Stetho.initializeWithDefaults(这个); 连接您的设备,运行应用程序,并在Chrome上进入以下站点: chrome: / /检查/ #设备

就是这样。现在可以探索您的表了。

注意:建议在转移到生产环境之前删除依赖项。

最后,我找到了一个最简单的解决方案,不需要打开DDMS。

实际上,解决方案是基于@Distwo提到的,但它不必那么复杂。

首先,记住数据库文件在设备中的路径,它应该始终相同。 例如我的是:/data/data/com.XXX.module/databases/com.XXX.module.database

其次,执行这个命令,将数据库文件拉到PC上

 adb pull /data/data/com.XXX.module/databases/com.XXX.module.database /Users/somePathOnYourPC/

您需要做的就是复制并存储这个命令,然后您就可以一次又一次地使用它。

第三,如果你得到了Permission Denied或类似的情况,只需在前面的命令之前运行adb root。

在Android 4.1 Canary 5预览版中引入了新的数据库检查器,您现在可以直接从IDE - https://developer.android.com/studio/preview/features?linkId=86173020#database-inspector检查、查询、修改和调试正在运行的应用程序中的SQLite数据库

支持数据库实时查询以及- https://developer.android.com/studio/preview/features?linkId=86173020#query

我把这个过程的unix命令行自动化放在一起,并把代码放在这里:

https://github.com/elliptic1/Android-Sqlite3-Monitor

它是一个shell脚本,以包名和数据库名作为参数,从附加的Android设备下载数据库文件,并针对下载的文件运行自定义脚本。然后,使用像“watch”这样的unix工具,你可以打开一个终端窗口,定期更新数据库脚本输出的视图。

它是其他答案加加号的组合

Install DB Browser for SQLite Get the exact location from Device File Explorer for MyBase.db something like "/data/data/com.whatever.myapp/databases/MyBase.db" Set the project window from Android to Project - so you can see the files (build.gradle, gradle.properties,...etc) In Project window right click and chose Open in Terminal In terminal you are now in the root director of your application from the hardisk so execute: adb pull /data/data/com.whatever.myapp/databases/MyBase.db Now on the Project window of you Android Studio you have a file "MyBase.db" Double click your db file from Project and you can now browse/edit your databases in DB Browser When you are ready just do Write Changes in DB Browser so databases are saved on hardisk In terminal with the command: adb push MyBase.db /data/data/com.whatever.myapp/databases/MyBase.db you just send from the Hardisk to device the edited database.