查看使用 Room Persistence Library 创建的数据库的内容

View contents of database created with Room Persistence Library

有没有更简单的方法来查看在 Android Studio 中使用 Room Persistence Library 创建的数据库的内容?

Database Inspector 正式支持 Android Studio

以前,Database Inspector 开始包含在 Android Studio 4.1 Canary 频道中,现在它已经内置在最新版本的 Android Studio Arctic Fox 中。

现在,Database Inspector 位于 App Inspection 选项卡下。 您必须选择连接的设备,然后需要选择要检查数据库的包名称。

左侧显示可用的table需要双击查看table详情,右侧显示.

选项 1

您可以使用 Android-Debug-Database,您可以从浏览器中对您的数据进行 CRUD,然后您可以从浏览器中查看您的首选项数据。

选项 2

如果你不想从浏览器使用并且你必须检查其他文件,需要检查你的数据更改,使用 Genymotion Emulator.So 你必须将你的 emulator.Try root 到 Root您的模拟器,请参阅 。 如果您不满意,请告诉我。

旧 Android Studio 版本:

Emulator -> Android Studio -> Device File Explorer -> /data/data/{$packageId}/databases/ -> Save As -> https://sqlitebrowser.org/

在以后的 Android Studio 版本 (3.5+) 中:

View -> Tool Windows -> Device File Explorer -> /data/data/{$packageId}/databases/ -> Save As -> https://sqlitebrowser.org/

在 android 工作室 3.1.*

在工具window栏中点击“Device File explorer”,通常你可以在屏幕的右下角找到它

打开 data/data/your-application-package/databases

中的目录

使用新架构在数据库目录中创建了 3 个文件

your-database-name
your-database-name-shm
your-database-name-wal

您必须将所有 3 个导出到同一目录

然后在任何 sqlite 浏览器中打开第一个文件(仅包含您的数据库名称)。

现在您可以看到您的所有数据......

your-database-name-shm
your-database-name-wal

打开db文件需要这两个额外的文件 如果您只打开数据库文件,那么您将不会在该文件中找到任何 table

我个人使用Android Debug Database

Android Debug Database is a powerful library for debugging databases and shared preferences in Android applications.

快速入门

将此添加到您应用的 build.gradle

debugImplementation 'com.amitshekhar.android:debug-db:1.0.4'

只需启动应用程序,您将在 logcat 中看到如下条目:

D/DebugDB: Open http://192.168.1.104:8080 in your browser



有关详细信息,请查看 github repo

我是按照以下步骤完成的:

1) 从下面link.

下载数据库浏览器

https://sqlitebrowser.org/

2) 从 IDE 转到设备文件资源管理器 & 复制生成的三个文件,如 enter image description here 和 select 所有三个文件: 你的-db,你的-db-shm,你的-db-wal

按 ctrl+shift+s 并将其粘贴到某个文件夹。完成。

3) 最后用 DbBrowser App 打开你的数据库。

下载并安装DB Browser for SQLite

在 Android Studio 版本 >= 3.0 中:

通过以下方式打开设备文件资源管理器:

查看>工具Windows>设备文件资源管理器

在“设备文件资源管理器”中转到:

数据 > 数据 > PACKAGE_NAME > 数据库

其中 PACKAGE_NAME 是您的包的名称(在下面的示例中是 com.edgedevstudio.sample)

右键单击数据库并select另存为。 将其保存在 PC 上的任何位置。

打开 SQLite 数据库浏览器并单击 'open Database' 并打开数据库。

In the case of the example above, you should locate "todolist" NOT "todolist-shm" nor "todolist-wal"

If you do not export the 3 database files (db_name, db_name-shm & db_name-wal, where db_name = name of the database) you will not be able to read the database.

使用Stetho.

将其作为 dependency 添加到您的 Android 项目。

在您的 Application 中调用 Stetho.initializeWithDefaults(this) 并使用 chrome 检查工具查看您的数据库。在 chrome URL 栏中输入以下内容:chrome://inspect

急!你可以看到你的Room database

You can see your database table on the DB Browser for SQLite & room DB.

 In android studio 
1. Click on View -> Tool Windows -> Device File Explorer -> data
2. Select your project package name -> database -> select all file and save on desktop

Install **DB browser for SQLITE** 
1. sudo apt-get install sqlitebrowser //write on your terminal for install DB browser
2. install DB browser for sqlite
3. click on Open database and select file where you had saved your data
4. click on Brower Data and see your tables

This is all where i have see my android room database

我最近尝试了 Sethto By Facebook

Stetho is a sophisticated debug bridge for Android applications. When enabled, developers have access to the Chrome Developer Tools feature natively part of the Chrome desktop browser

您只需要包含它的 gradle 依赖项并在 MainApp 中调用。而已。 您可以使用 chrome://inspect/ 访问 chrome window 中的所有工具,包括您的 sqlite 数据库。

以下是 房间数据库 在模拟器上检查的屏幕截图。到目前为止我只在模拟器中试过。

按照上述评论从设备文件资源管理器获取数据库(3 个文件)后

我真的推荐简单的程序,比如使用 "DB Browser for SQLite" https://sqlitebrowser.org/

为了简单,请按照以下步骤操作:

  1. 下载 SQLiteBrowser 程序:Download SQLite for all OS
  2. 单击“Device Exploler”Android Studio 的右下角。
  3. 您会看到很多文件。不要担心!按照路径:data/data/{您的应用程序包名称}
  4. 右键单击“数据库”并在您的计算机中另存为。
  5. 打开 SQLiteBrowser 并单击“打开数据库”,然后选择与您的数据库同名的文件。
  6. 您可以看到与您的数据库 table 同名的文件和 select。
  7. 最后一步,您可以在“数据库结构”选项附近看到“浏览数据”选项。当你select它时,你可以看到你的数据库数据。

您可以看到步骤并按照下图中的数字进行操作

使用 Android Studio 4.1 Canary 6 及更高版本,您可以使用新的数据库检查器检查、查询和修改应用程序的数据库。例如,您可以通过修改数据库中的值并在设备上测试这些更改来调试 运行 应用程序。

https://developer.android.com/studio/preview/features#database-inspector

Android Studio 4.1 Canary 的最新版本中,Android Studio 提供了名为 Database Inspector 的新工具。

您可以使用此 Database Inspector 工具查看数据库文件及其内容,您还可以编辑数据库内容。

如果您正在使用 Room Persistence,那么它还会在 Android Studio 中为 运行 @Query 提供便利。可以看到@Query注解左边的运行按钮。

如果您正在使用 LiveData 那么它将实时反映数据库内容的变化,

在最新版本的 Android Studio 中,您可以在 App Inspection 工具 Window

中使用 Database Inspector



转到 App Inspection。然后是select的过程。 Database Inspector 现在将显示数据库内容。

您现在可以在 Android Studio

的数据库检查器中查看它

下图

Database Inspector Tab in Android Studio

Google官方: 现在有一种超级简单的方法可以直接从 Android Studio 查看 Room DB。如此简单,你会笑=) 看看这个:https://developer.android.com/studio/inspect/database

Database Inspector
在新的 Android Studio Arctic Fox
点击App检查
Select 选项卡中的数据库检查器
右键单击数据库名称
单击导出为文件
下载后您在 DB Browser for SQLite Application
中查看数据库 *注意 Database Inspector 仅当应用程序部署在 API 26

时才有效

在 android studio 中以调试模式查看、编辑、添加和删除您的房间数据库最方便。

Implement dependency in build.gradle

debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'

Copy paste below code in your activity

public static void showDebugDBAddressLog() {
    if (BuildConfig.DEBUG) {
        try {
            Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
            Method getAddressLog = debugDB.getMethod("getAddressLog");
            Object value = getAddressLog.invoke(null);
            Log.d("link", "showDebugDBAddressLogToast: "+(String) value);
        } catch (Exception ignore) {

        }
    }
}

Call Activity in oncreate.

showDebugDBAddressLog();

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Run The app in dubug mode

Search in logcat

link

You see the link in logcat

showDebugDBAddressLog: Open http://192.168.1.100:8080 in your browser

Open link "http://192.168.1.100:8080" in your browser.

繁荣你看到你所有的数据库。谢谢

遵循 Android

中的以下文档

https://developer.android.com/studio/inspect/database