数据库检查器

Creating and managing local databases is a core component of most mobile apps. But whether you’re using SQLite directly or through the Room persistence library, Android developers have been asking for a better way to inspect and debug databases in their running app. To learn more watch the video or read the blog below.

创建和管理本地数据库是大多数移动应用程序的核心组件。 但是,无论您是直接使用SQLite还是通过Room持久性库使用,Android开发人员一直在寻求更好的方法来检查和调试正在运行的应用程序中的数据库。 要了解更多信息,请观看视频或阅读下面的博客。

The latest version of Android Studio 4.1 (currently available in Canary) comes with a new tool called Database Inspector, which helps you inspect, query, and modify databases in your running app.

最新版本的Android Studio 4.1(目前在Canary中可用)附带了一个名为Database Inspector的新工具, 这可以帮助您检查,查询和修改正在运行的应用程序中的数据库。

Database Inspector helps you to modify the data just like editing a spreadsheet.
Database Inspector helps you to modify the data just like editing a spreadsheet.
数据库检查器可以像编辑电子表格一样帮助您修改数据。

With Database Inspector, modifying the data in your database is not much harder than editing a spreadsheet; if you are using Room and observing the query results, the changes will be reflected instantly in your app!

使用Database Inspector,修改数据库中的数据并不比编辑电子表格难得多。 如果您正在使用Room并观察查询结果,那么更改将立即反映在您的应用程序中!

In this post, we will use the Database Inspector with the Sunflower app to test some edge cases. Sunflower is a gardening app that illustrates Android development best practices with Android Jetpack. I encourage you to clone the repo and follow along as you read this post.

在本文中,我们将结合使用Database Inspector和Sunflower应用程序来测试一些极端情况。 Sunflower是一个园艺应用程序,用于说明Android Jetpack的Android开发最佳实践。 我鼓励您克隆存储库,并在阅读本文时继续进行。

开始吧 (Get started)

The UI of the Sunflower app includes two tabs. To add some plants to my garden, let’s take a look at the Plant List tab, which lists available plants for my garden. On the top right, there is a filter button. When I press this button, the following list of plants is displayed:

Sunflower应用程序的UI包含两个选项卡。 要将一些植物添加到我的花园中,让我们看一下“ 植物列表”选项卡,其中列出了我的花园中可用的植物。 在右上角有一个过滤器按钮。 当我按下此按钮时,将显示以下植物列表:

Image for post
Click to filter Plant List
单击以过滤工厂列表

Clearly, this button filters the plants based on some criteria. But let’s imagine I am not super familiar with this project and I want to figure out how the filtering works by using the Database Inspector.

显然,此按钮可根据某些条件过滤植物。 但是,让我们想象一下,我对这个项目不是很熟悉,我想通过使用数据库检查器来了解过滤的工作方式。

To open the Database Inspector in Android Studio, I need to select View > Tool Windows > Database Inspector from the menu bar.

要在Android Studio中打开数据库检查器,我需要从菜单栏中选择查看>工具窗口>数据库检查器

Select View > Tool Windows > Database Inspector from the menu bar to open Database Inspector.
Select View > Tool Windows > Database Inspector from the menu bar to open Database Inspector.
从菜单栏中选择查看>工具窗口>数据库检查器,以打开数据库检查器。

This brings up the Database Inspector.

这将启动数据库检查器。

Database Inspector window
Database Inspector window
数据库检查器窗口

Now, I need to run the app to a device running API level 26 or higher and then select the app process from the dropdown menu.

现在,我需要在运行API级别26或更高级别的设备上运行应用程序,然后从下拉菜单中选择应用程序进程。

Select the running app process from the dropdown menu.
Select the running app process from the dropdown menu.
从下拉菜单中选择正在运行的应用程序进程。

After I select the app process I want to inspect, the database schemas appear in the panel below. To see Sunflower database tables, I need to expand the …/databases/sunflower-db schema.

选择要检查的应用程序过程后,数据库模式将显示在下面的面板中。 要查看Sunflower数据库表,我需要扩展…/ databases / sunflower-db模式。

Once you select a process, the app databases are listed.
Once you select a process, the app databases are listed.
选择进程后,将列出应用程序数据库。

Let’s go back to the app and take a look at the filtered plant list. The list contains Avocado, Grape, Orange and Tomato. If I could sort the plant table by the plant name, it wouldn’t be hard to find Avocado. Let’s see if the Database Inspector can do that!

让我们回到该应用程序,看看过滤后的植物列表。 名单包括鳄梨葡萄桔子蕃茄 。 如果我可以按植物名称对植物表进行排序,那么找到鳄梨就不难了。 让我们看看数据库检查器是否可以做到这一点!

First, I double-click on the plants table to display its data. The data is displayed with a default page size of 50, but you can reduce this number and page between a shorter list of results instead. Clicking on the name column sorts the plant table on the name of the entries. As expected, Avocado is not far down the list and I find it in the second row of the table.

首先,我双击植物表以显示其数据。 数据显示的默认页面大小为50,但是您可以在较短的结果列表之间减少此数字和页面。 单击名称列可将工厂表按条目名称排序。 不出所料, 鳄梨不在列表的最下方,我在表的第二行中找到它。

查询数据库 (Query your databases)

Looking at the data entry of Avocado, the growZoneNumber is most likely the property which the app uses for filtering. To verify that, let’s run a query on the growZoneNumber, which is 9 for Avocado. Actually, this query already exists in PlantDao.kt and I can run the query directly from Room’s @Query annotations. Every @Query annotation has a small run icon next to its line number.

查看鳄梨的数据条目, growZoneNumber最有可能是应用程序用于过滤的属性。 为了验证这一点,让我们在growZoneNumber上运行查询对于Avocado来说是9。 实际上,该查询已经存在于PlantDao.kt中,我可以直接从Room的@Query批注中运行查询。 每个@Query注释的行号旁边都有一个小运行图标。

When I click the run icon for the query of getPlantsWithGrowZoneNumber() and select the correct database, a popup appears asking me the value for :growZoneNumber.

当我单击查询getPlantsWithGrowZoneNumber()的运行图标并选择正确的数据库时,将出现一个弹出窗口,询问我:growZoneNumber的值。

You can run queries directly from @Query annotation.
You can run queries directly from @Query annotation.
您可以直接从@Query批注运行查询。

I can enter a value of 9 and click Run to see the query results.

我可以输入值9并单击“运行”以查看查询结果。

Alternatively, I can type my own query and run it inside the tool window. This gives me more freedom, as I am not limited to the queries defined in DAO interfaces. To run my own query, I click Run SQL and select the plants database from the newly opened tab on the right.

另外,我可以键入自己的查询并在工具窗口中运行它。 这给了我更多的自由,因为我不仅限于DAO接口中定义的查询。 要运行自己的查询,请单击“运行SQL” ,然后从右侧新打开的选项卡中选择plants数据库。

Image for post
Click Run SQL and select the app database.
单击“运行SQL”,然后选择应用程序数据库。

Next, I type the following query into the box next to the database selection dropdown and click Run.

接下来,我在数据库选择下拉列表旁边的框中输入以下查询,然后点击运行

Select * from plants where growZoneNumber=9

从growZoneNumber = 9的植物中选择*

Image for post
You can execute SQL queries in Database Inspector
您可以在数据库检查器中执行SQL查询

Here it is! We have the exact same list of plants as is shown when the filter is on.

这里是! 我们具有与打开过滤器时完全相同的植物列表。

修改和调试数据库 (Modify and debug your database)

The Database Inspector makes debugging your app a breeze by allowing you to modify values in your app’s databases while it’s running on a device.

Database Inspector允许您在应用程序在设备上运行时修改应用程序数据库中的值,从而轻松调试应用程序。

First, I want to test the app UI for really long plant names. Instead of changing the data source and refreshing the database with the new data, I’ll edit the values directly on the database using the Database Inspector.

首先,我要测试应用程序用户界面中是否有很长的植物名称。 我将使用数据库检查器直接在数据库上编辑值,而不是更改数据源并使用新数据刷新数据库。

Image for post
You can edit your database in Database Inspector
您可以在数据库检查器中编辑数据库

Now that the cell is editable, I change the name from Apple to A really special type of Apple and press enter. If you are following along, you can type anything you like with the length you want to test in the app UI.

现在该单元格是可编辑的,我将名称从Apple更改为Apple一种非常特殊的类型,然后按Enter。 如果您遵循此步骤,则可以在应用程序用户界面中键入任何您想要测试的长度的内容。

Image for post
If your app uses Room and observes the query results, you don’t need to restart the app to see the changes.
如果您的应用使用Room并观察到查询结果,则无需重新启动应用即可查看更改。

Now let’s go back to the app. Notice that without us doing anything, the app is displaying the updated data! If your app uses Room and observes the query results (using LiveData/Flow) you do not need to trigger a database query to refresh the data. Otherwise, depending on how your app triggers a query, you may need to relaunch the app or just navigate to the related activity/fragment again. In order to use the full potential of the Database Inspector this might be a good excuse to migrate your app to use LiveData or Flow.

现在,让我们回到该应用程序。 请注意,没有我们做任何事情,该应用程序将显示更新的数据! 如果您的应用使用Room并观察到查询结果(使用LiveData / Flow),则无需触发数据库查询即可刷新数据。 否则,根据您的应用触发查询的方式,您可能需要重新启动该应用或仅再次导航到相关活动/片段。 为了充分发挥Database Inspector的潜力,这可能是迁移您的应用程序以使用LiveData或Flow的好借口。

Looking back at our app, it looks like the card view is not designed to handle such long plant names. I’ll fix this later, but let’s move on to the next test.

回顾我们的应用程序,看似卡片视图并非旨在处理如此长的植物名称。 稍后将解决此问题,但让我们继续下一个测试。

Image for post
The app doesn’t seem to handle long text well.
该应用似乎无法很好地处理长文本。

Each plant has a different watering interval and I want to see what happens when watering day is past due. To do this, I need to add some plants to my garden; but first, I select the Live updates checkbox in the Database Inspector. When Live updates is checked, the Database Inspector automatically displays any changes your app makes to its database.

每个植物都有不同的浇水间隔,我想看看何时浇水到期。 为此,我需要在花园里添加一些植物。 但是首先,我在数据库检查器中选中了“ 实时更新”复选框。 选中实时更新后 ,数据库检查器会自动显示您的应用对其数据库所做的任何更改。

Image for post
Select Live Updates.
选择实时更新。

I go back to the My Garden tab and add some plants such as Avocado and Eggplant; but first, I go back to the Database Inspector and double click on garden_plantings to observe the table. Notice how the data in garden_plantings table is updated automatically as I add new plants.

我回到“ 我的花园”选项卡,添加一些植物,例如鳄梨茄子。 但是首先,我回到数据库检查器并双击garden_plantings来观察表。 请注意,在添加新植物时,garden_plantings表中的数据是如何自动更新的。

Image for post
Database Inspector can automatically display changes.
数据库检查器可以自动显示更改。

Both these plants have a watering interval of 3 days. I don’t really want to wait 3 days to see what happens so I’ll edit the database and change the last_watering_day. Once again I go back to the Database Inspector and double click on garden_plantings. The last_watering_date is the last column of the table. I’ll change the values of both records to something smaller to reflect sometime before today’s date.

这两种植物的浇水间隔均为3天。 我真的不想等待三天才能看到会发生什么,所以我将编辑数据库并更改last_watering_day 。 我再次回到数据库检查器,然后双击garden_plantingslast_watering_date是表的最后一列。 我将这两个记录的值更改为较小的值,以反映今天的日期之前的某个时间。

Image for post
Edits to your databases are visible instantly!
对您数据库的编辑立即可见!

Ok, it looks like I went a little too far back in time, but that should still work for my test. The app UI seems fine displaying the due date. For future development, maybe we can suggest adding a warning for the user when watering day is passed.

好的,看来我倒退了一点时间,但是对于我的测试来说仍然可以使用。 应用界面似乎可以很好地显示截止日期。 为了将来的发展,也许我们建议在浇水日过后为用户添加警告。

Go ahead and give the new Database Inspector a try! Let us know what you think; and if you come across any issues, please don’t forget to file bugs!

继续尝试新的数据库检查器! 让我们知道您的想法; 如果您遇到任何问题,请不要忘记提交错误

翻译自: https://medium.com/androiddevelopers/database-inspector-9e91aa265316

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值