在Android Studio中探索数据库检查器

Android Studio团队在Canary 4.1版本中引入了数据库检查器,为SQLite和Room数据库调试提供便利。开发者可以同时安装多个版本的Android Studio,如稳定版和Canary版,以体验新功能。
摘要由CSDN通过智能技术生成

Whether you’re working with an SQLite or Room database, every developer needs a tool to debug it. Recently, Android Studio’s team released a new version (Canary 4.1) with the database inspector feature. Keep in mind that it was only introduced in Canary 4.1 and is thus still in its early stages.

无论您使用的是SQLite数据库还是Room数据库,每个开发人员都需要一个工具来对其进行调试。 最近,Android Studio团队发布了具有数据库检查器功能的新版本(Canary 4.1)。 请记住,它仅在Canary 4.1中引入,因此仍处于早期阶段。

You can install Canary 4.1 from this link. Also, remember that you can run multiple versions of Android Studio on your laptop simultaneously (e.g. Android Studio’s stable version for professional development and Canary versions to explore upcoming features like the database and layout inspectors).

您可以从此链接安装Canary 4.1。 另外,请记住,您可以同时在笔记本电脑上运行多个版本的Android Studio(例如,用于专业开发的Android Studio的稳定版和用于浏览数据库和布局检查器等即将推出的功能的Canary版本)。

调用数据库检查器 (Invoking Database Inspector)

Once you are finished with the IDE setup, the next step would be to open a project with a local database feature. If you don’t have any project with local DB, then use the sunflower app.

一旦完成IDE设置,下一步就是打开具有本地数据库功能的项目。 如果您的本地数据库没有任何项目,请使用sunflower应用程序。

After opening the project, run the app in the emulator or device. Then to open the database inspector, navigate as shown below:

打开项目后,在模拟器或设备中运行应用程序。 然后打开数据库检查器,如下所示进行导航:

Image for post
View -> Tool windows -> Database Inspector
查看->工具窗口->数据库检查器

选择要调试的设备和数据库 (Select Device and Database to Debug)

Once you click on the database inspector, a new window opens up in the studio. First, you have to select the device if you connected multiple devices to the system. If your app has multiple databases, then you need to select the database that you want to debug. Have a look:

单击数据库检查器后,工作室中将打开一个新窗口。 首先,如果您将多个设备连接到系统,则必须选择设备。 如果您的应用程序具有多个数据库,则需要选择要调试的数据库。 看一看:

Image for post
Database inspector
数据库检查器

在数据库检查器中执行查询 (Executing Queries in Database Inspector)

This is the exciting part where you can see the data in the local DB on the database inspector by executing queries. You can execute them in two ways — Dao queries and custom queries using the Run Query option.

这是令人兴奋的部分,您可以通过执行查询在数据库检查器上查看本地数据库中的数据。 您可以通过两种方式执行它们-道查询和使用“运行查询”选项的自定义查询。

道查询 (Dao queries)

We can actually run Dao functions using a database inspector and see the data. This feature comes in handy while you’re debugging. When we open any Dao class after invoking a database inspector, you’ll see a Run symbol beside each function to execute the queries in the database inspector. Have a look:

实际上,我们可以使用数据库检查器运行Dao函数并查看数据。 调试时,此功能非常方便。 在调用数据库检查器之后打开任何Dao类时,您将在每个函数旁边看到一个运行符号,以在数据库检查器中执行查询。 看一看:

Image for post
Executing Dao queries
执行Dao查询

自定义查询 (Custom queries)

Android Studio’s team has taken great care to cover basic needs in the database inspector. One of which is the ability to run custom queries. This can help to debug the database in multiple ways without actually writing anything in the Dao. Have a look:

Android Studio团队非常注意满足数据库检查器中的基本需求。 其中之一是运行自定义查询的能力。 这可以帮助以多种方式调试数据库,而无需实际在Dao中编写任何内容。 看一看:

Image for post
Executing custom queries
执行自定义查询

更新资料 (Update Data)

If you can’t update the data from the inspector, all the features above won’t be much of use. So the Android Studio team also included a way to alter or add data to the database from the inspector itself. You can see the changes in the device in a matter of seconds if you’re using Room and observing data through livedata/flow.

如果您无法从检查器中更新数据,则上述所有功能都将无法使用。 因此,Android Studio团队还提供了一种从检查器本身更改或向数据库添加数据的方法。 如果您正在使用Room并通过livedata / flow观察数据,则可以在几秒钟内看到设备中的更改。

This feature is useful in many situations while debugging. Let’s say we have the name of a plant with 10 characters, and you want to increase the length to 40 characters to check UI. In this use case, you can directly update the name from the inspector and check the UI.

在调试时的许多情况下,此功能很有用。 假设我们有一个具有10个字符的植物名称,并且您想将长度增加到40个字符以检查UI。 在这种情况下,您可以直接从检查器更新名称并检查UI。

To update the data, you just need to double-click on the field and add/remove the data. After that, press Enter to update it in the database. Have a look:

要更新数据,只需双击该字段并添加/删除数据。 之后,按Enter键以在数据库中对其进行更新。 看一看:

Image for post
Updating data from inspector
从检查器更新数据

实时更新 (Live Updates)

This is one of my favorite features both in the database and the recently released layout inspector. With this feature, if there any changes in the database, we can see them in the inspector instantly.

这是数据库和最近发布的布局检查器中我最喜欢的功能之一。 使用此功能,如果数据库中有任何更改,我们可以立即在检查器中看到它们。

To enable this feature first, we need to 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. This feature brings robust behavior to the database inspector.

要首先启用此功能,我们需要选择“实时更新” 数据库检查器中的复选框。 选中“实时更新”后,数据库检查器会自动显示您的应用对其数据库所做的任何更改。 此功能为数据库检查器带来了强大的行为。

Image for post
Enable live updates in database inspector
在数据库检查器中启用实时更新

结论 (Conclusion)

That is all for now. Thanks for reading! I hope you learned something useful.

到此为止。 谢谢阅读! 我希望你学到了一些有用的东西。

翻译自: https://medium.com/better-programming/exploring-the-database-inspector-in-android-studio-de0026024895

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值