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

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

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

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


当前回答

在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

其他回答

经过所有的解决方案,我将建议

使用Stethos 让我们看看它有多简单

在构建中添加以下依赖项。gradle文件

implement 'com.facebook.stetho:stetho:1.5.0'
implement 'com.facebook.stetho:stetho-js-rhino:1.4.2'

然后转到mainActivity onCreate方法 添加以下行

Stetho.initializeWithDefaults(this);

这需要你

import com.facebook.stetho.Stetho;

现在在运行应用程序从android工作室,打开Chrome和地址栏类型 chrome: / /检查/

在资源选项卡>web SQL 检查数据库和表 玩它在实时容易

更新:在最近的Chrome版本中,Stethos被打破了。如果遇到问题,请尝试使用Edge开发工具代替Edge://inspect/#devices -参见:https://stackoverflow.com/a/67674265/14232521

虽然这是一个很老的问题,但我认为它今天仍然有意义。在最新版本的android studio中,我找到了一种方法来查看它(我不确定以前的版本中是否可用)。]

下面是如何查看数据库中的数据:

步骤:

Go to View > Tool Windows > Device File Explorer Then the folder data > data and you will see applications lists. There scroll down a little bit and see the package [ your package for application ]. After finding your package, see databases folder and select file, right click it and save [ download ] As you can't see the data directly, there is a tool, DB Browser for SQLite, go website https://sqlitebrowser.org/ Open Db browser and click open database and choose the database table you downloaded and it will show you the data.

新编辑

现在(当你阅读这篇文章的时候),这甚至更容易了,因为Android工作室已经提供了数据库检查器(就在右下角附近)。

点击它,你会看到数据库。 但是,请确保API的最小级别是26,如果不通过构建更改它。gradle文件作为minSdkVersion 26

在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

我使用windows 7,我的设备是一个模拟的android设备API 23。我想对于任何真实的设备都是一样的,只要它是根设备,API不超过23

进入工具-> Android -> Android设备监视器。 进入文件资源管理器。在我的例子中,它在data/data//app_webview/databases/file_0/1中

我必须手动在名为"1"的文件末尾添加。db

最后,我找到了一个最简单的解决方案,不需要打开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。