简体   繁体   English

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

[英]View contents of database created with Room Persistence Library

Is there any easier way to see the contents of database created with Room Persistence Library in Android Studio?在 Android Studio 中,有没有更简单的方法来查看使用 Room Persistence Library 创建的数据库的内容?

In older Android Studio versions:在较旧的 Android Studio 版本中:

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

In later Android Studio versions (3.5+):在更高的 Android Studio 版本(3.5+)中:

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

in android studio 3.1.*在 android studio 3.1.*

in tool window bar click on " Device File explorer " generally you can find this in bottom right corner of the screenn在工具窗口栏中单击“ Device File explorer ”,通常您可以在屏幕右下角找到它

open directory in data/data/your-application-package/databasesdata/data/your-application-package/databases打开目录

with new architecture 3 files is created in databases directory使用新架构在数据库目录中创建 3 个文件

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

you have to export all 3 in the same directory您必须将所有 3 个导出到同一目录中

then open first one file (that is with your-database-name only ) in any sqlite browser.然后在任何 sqlite 浏览器中打开第一个文件(仅使用 your-database-name )。

and now you can see all your data .......现在你可以看到你所有的数据......

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

these two extra files are needed to open db file if you will open database file only, than you will not found any table in that file如果您只打开数据库文件,则需要这两个额外的文件来打开 db 文件,而不是在该文件中找不到任何表

Method 1方法一

You can use Android-Debug-Database , and you can CRUD of your data from a browser, and then you can see your Preference data from a browser.您可以使用Android-Debug-Database ,您可以从浏览器 CRUD 数据,然后您可以从浏览器查看您的首选项数据。

Method 2方法二

If you don't want to use from Browser and you have to check other files,need to check your data changes, use a Genymotion Emulator.So you have to root your emulator.Try to Root your emulator, plz see in https://stackoverflow.com/a/44039429/2772552 .如果您不想从浏览器使用并且您必须检查其他文件,需要检查您的数据更改,请使用 Genymotion 模拟器。所以您必须将您的模拟器设为 root。尝试将您的模拟器设为 Root,请参阅https:/ /stackoverflow.com/a/44039429/2772552 Let me know if you are not OK.如果您不满意,请告诉我。

Download & Install DB Browser for SQLite下载并安装适用于 SQLite 的数据库浏览器

In Android Studio versions >= 3.0:在 Android Studio 版本 >= 3.0 中:

Open Device File Explorer via:通过以下方式打开设备文件资源管理器:

View > Tool Windows > Device File Explorer视图>工具窗口>设备文件资源管理器

In "Device File Explorer" Go to:在“设备文件资源管理器”中转到:

data > data > PACKAGE_NAME > databases数据>数据> PACKAGE_NAME >数据库

where PACKAGE_NAME is the name of your package (it is com.edgedevstudio.sample in the example below)其中 PACKAGE_NAME 是您的包的名称(在下面的示例中为 com.edgedevstudio.sample)

设备文件资源管理器

Right click on the database and select Save As.右键单击数据库并选择另存为。 Save it anywhere you want on your PC.将其保存在 PC 上任何您想要的位置。

Open, DB Browser for SQLite & click 'open Database' and open the database.打开 SQLite 的数据库浏览器并单击“打开数据库”并打开数据库。 在此处输入图片说明

In the case of the example above, you should locate " todolist " NOT "todolist-shm" nor "todolist-wal"在上面的例子中,你应该找到“ todolist ”而不是“todolist-shm”或“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.如果您不导出 3 个数据库文件(db_name、db_name-shm 和 db_name-wal,其中 db_name = 数据库名称),您将无法读取数据库。

Personally i use Android Debug Database我个人使用Android 调试数据库

Android Debug Database is a powerful library for debugging databases and shared preferences in Android applications. Android Debug Database 是一个强大的库,用于在 Android 应用程序中调试数据库和共享首选项。

Quick start快速开始

Add this to your app's build.gradle将此添加到您的应用程序的 build.gradle

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

Just start the application, you will see in the logcat an entry like follows :只需启动应用程序,您将在 logcat 中看到如下条目:

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

在此处输入图片说明


在此处输入图片说明


For more information have a look at the github repo有关更多信息,请查看 github repo

For easy way, follow these steps:为简单起见,请按照以下步骤操作:

  1. Download SQLiteBrowser program: Download SQLite for all OS下载 SQLiteBrowser 程序:下载适用于所有操作系统的 SQLite
  2. Click "Device Exploler" where is Right-Bottom corner on Android Studio.单击 Android Studio 右下角的“Device Exploler”。
  3. You will see a lot of files.你会看到很多文件。 Do not worry!别担心! Follow path: data/data/{your app package name}遵循路径:data/data/{您的应用程序包名称}
  4. Right click on "databases" and Save As in your computer.右键单击“数据库”并在您的计算机中另存为。
  5. Open SQLiteBrowser and click "Open Database" than choose file which has same name with your database name.打开 SQLiteBrowser 并单击“打开数据库”,然后选择与您的数据库名称同名的文件。
  6. You can see files and select which has same name with your database table name.您可以查看文件并选择与您的数据库表名同名的文件。
  7. Final step, you can see "Browse Data" option near of Database Structure" option. When you select it, you can see your data of database.最后一步,您可以在“数据库结构”选项附近看到“浏览数据”选项。选择它时,您可以看到您的数据库数据。

You can see steps and just follow numbers on below image您可以看到步骤,只需按照下图的数字即可

您可以在图像上看到步骤。

With the latest release of the Android Studio 4.1 Canary , Android Studio provide new tool called Database Inspector .随着最新发布的Android Studio 4.1 Canary ,Android Studio 提供了名为Database Inspector 的新工具。

You can use this Database Inspector tool to view the database file and it's content, you can also edit database content.您可以使用此数据库检查器工具查看数据库文件及其内容,还可以编辑数据库内容。

在此处输入图片说明

If you are using Room Persistence then it also provide facility to run @Query with in the Android Studio.如果您使用的是Room Persistence那么它还提供了在 Android Studio 中运行@Query工具。 You can see the run button of the left side of the @Query annotation.可以看到@Query注解左侧的运行按钮。

在此处输入图片说明

If you are using LiveData then it will live reflect database content changes,如果您使用的是LiveData那么它将实时反映数据库内容的变化,

在此处输入图片说明

Use Stetho .使用听诊器

Add it to your Android project as a dependency .将其作为dependency添加到您的 Android 项目中。

Call Stetho.initializeWithDefaults(this) in your Application and view your database using the chrome inspect tool.在您的Application调用Stetho.initializeWithDefaults(this)并使用 chrome 检查工具查看您的数据库。 Enter the following in your chrome URL bar: chrome://inspect .在您的 chrome 网址栏中输入以下内容: chrome://inspect

Presto!快! You can see your Room database .你可以看到你的Room database

I did this by following steps:我通过以下步骤做到了这一点:

1) Download the db browser from following link. 1) 从以下链接下载数据库浏览器。

https://sqlitebrowser.org/ https://sqlitebrowser.org/

2)Go to Device File Explorer from IDE & copy three file generated like enter image description here and select all three files : your-db, your-db-shm, your-db-wal 2)从IDE转到设备文件资源管理器并复制生成的三个文件,例如在此处输入图像描述并选择所有三个文件:your-db,your-db-shm,your-db-wal

press ctrl+shift+s and paste it to some folder.按 ctrl+shift+s 并将其粘贴到某个文件夹。 Done.完成。

3) finally open the your-db with DbBrowser App. 3) 最后用 DbBrowser App 打开 your-db。

With Android Studio 4.1 Canary 6 and higher, you can inspect, query, and modify your app's databases using the new Database Inspector.借助 Android Studio 4.1 Canary 6 及更高版本,您可以使用新的数据库检查器检查、查询和修改应用的数据库。 For example, you can debug your running app by modifying values in your database and testing those changes on the device.例如,您可以通过修改数据库中的值并在设备上测试这些更改来调试正在运行的应用程序。

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

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

In latest versions of Android Studio, you can use Database Inspector inside App Inspection Tool Window在最新版本的 Android Studio 中,您可以在App Inspection工具窗口中使用Database Inspector

在此处输入图片说明



Go To App Inspection .转到App Inspection Then select the process.然后选择进程。 Database Inspector will now show the database contents. Database Inspector现在将显示数据库内容。

在此处输入图片说明

You can now view it in Database Inspector in Android Studio您现在可以在 Android Studio 的 Database Inspector 中查看它

Image below下图

Database Inspector Tab in Android Studio Android Studio 中的数据库检查器选项卡

I recently tried Sethto By Facebook .我最近尝试了Sethto By Facebook

Stetho is a sophisticated debug bridge for Android applications. Stetho 是一个复杂的 Android 应用程序调试桥。 When enabled, developers have access to the Chrome Developer Tools feature natively part of the Chrome desktop browser启用后,开发人员可以访问 Chrome 桌面浏览器本机部分的 Chrome 开发人员工具功能

You just need to include it's gradle dependency and call in MainApp.您只需要包含它的 gradle 依赖项并在 MainApp 中调用。 That's it.就是这样。 You get access to all the tools in chrome window by using chrome://inspect/ including your sqlite database.您可以使用chrome://inspect/访问 chrome 窗口中的所有工具,包括您的 sqlite 数据库。

Following is a screenshot where inspected by Room Database on Emulator.以下是模拟器上Room Database检查的屏幕截图。 I only tried in emulator so far.到目前为止,我只在模拟器中尝试过。

在此处输入图片说明

after getting your DB (3 files) from Device File Explorar as comments said above从设备文件浏览器获取您的数据库(3 个文件)后,如上文所述

I really recommend easy program like using "DB Browser for SQLite" https://sqlitebrowser.org/我真的推荐简单的程序,比如使用“DB Browser for SQLite” https://sqlitebrowser.org/

Google official: Nowadays there's a super simple way to view your Room DB straight from Android Studio.谷歌官方:现在有一种超级简单的方法可以直接从 Android Studio 查看你的 Room DB。 So simple you will laugh =) Check this out: https://developer.android.com/studio/inspect/database如此简单你会笑 =) 看看这个: https : //developer.android.com/studio/inspect/database

Database Inspector数据库检查员
In the new Android Studio Arctic Fox在新的 Android Studio 北极狐中
Click on the App inspection点击App检查
Select Database Inspector from the tab从选项卡中选择数据库检查器
Right Click on the Database name右键单击数据库名称
Click Export as a File单击导出为文件
After Download you View the database in DB Browser for SQLite Application下载后,您在SQLite应用程序的DB 浏览器中查看数据库
*Note Database Inspector works only if the app deployed on API 26 *注意数据库检查器仅在应用程序部署在 API 26 上时才有效

Best and easy to view, edit, add & delete your roomdatabase in debug mode in android studio.在 android studio 的调试模式下,最佳且易于查看、编辑、添加和删除您的房间数据库。

Implement dependency in build.gradle在 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.在 oncreate 中调用 Activity。

showDebugDBAddressLog();

Add permission in AndroidManifest.xml在 AndroidManifest.xml 中添加权限

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

Run The app in dubug mode在dubug模式下运行应用程序

Search in logcat在 logcat 中搜索

link

You see the link in logcat您在 logcat 中看到链接

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

Open link "http://192.168.1.100:8080" in your browser.在浏览器中打开链接“http://192.168.1.100:8080”。

Boom you see your all database.繁荣你看到你的所有数据库。 Thank You谢谢你

Follow the below documentation from Android遵循以下来自 Android 的文档

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

In android atudio you'll find a tab in the bottom "App Inspector" There is a seperate tab for Database inspection.在 android atudio 中,您会在底部的“App Inspector”中找到一个选项卡,其中有一个单独的选项卡用于数据库检查。 From there you can see all the databases in the device从那里你可以看到设备中的所有数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM