Hello everyone, I am bingxin said. We’re here today with a new feature in Android Studio 4.1.

The Sqlite database is certainly familiar to most Android developers. I remember the first few interviews in my life, but also encountered a request to write a freehand add, delete, modify and check the client. So, how do you generally view or debug databases?

How do you view and debug databases?

As we all know, database files are stored in the application sandbox and cannot be viewed directly. It can also be exported from Android Studio’s Device File Explorer. It can also be exported from PC through any database software, such as Navicat, etc.

This can be a hassle, but some of the best open source libraries in the industry offer a solution: Android-debug-database, github:

https://github.com/amitshekhariitbhu/Android-Debug-Database

After integration in the project, you can directly view, modify, and edit the current application database file in the browser side. Not only database files, but also SharedPreference files can be manipulated. Since they are all files in private directories, the principle is the same. Here is a screenshot of the browser shown in the README:

DoraemonKit

Too much trouble?

What? Look at a database and you want me to integrate a library? You want me to open my browser? Don’t you know Chrome eats memory?

In fact, this kind of mindless work should be done by an IDE. On Android Studio 4.1 Canary 5 and later, Database Inspector is built in and provides the following features:

  • Query and modify table data
  • Execute a query statement
  • Execute the Room query defined in the Dao file

Let me take the sunflower project, which is familiar to you, as an example to demonstrate the above functions.

Use the Database Inspector

View and modify table data

The opening mode is as shown in the figure below:

The Database Inspector automatically lists the currently debuggable process, along with all tables in the Database that that process’s application contains. The sunflower app, as shown below, contains garden_plantings and Plants, which represent the plants in my garden and all plants, respectively.

All databases of the application will be listed on the left. Double-click the database and all tables under the database will be displayed in the drop-down list. Double-click the table to display all the data in the current table in the right view.

Any data in the table is modifiable. Double click on any of them, and then press Enter to save. When you refresh the page on the App, you can see the change.

Instead, you can make changes on the App that involve Database operations and just click Refresh in the Database Inspector to see the changes. (Note that this isn’t real time, you’ll have to manually refresh to see the changes).

You can refresh the garden_plantings table after adding plants to my garden and see the changes immediately.

Execute SQL

In addition to viewing and modifying table data, The Database Inspector provides the ability to execute SQL statements, a feature that is available in most PC Database software. Take a look at the GIF below:

Combining with the Room

Room, Google’s own son, deserves preferential treatment. The interface methods that you tag in Room with the @Query annotation can be executed directly in Android Studio and displayed directly in the Database Inspector. Take a look at the GIF below:

As you can see from the GIF above, when the method being executed needs to query for parameters, it asks us to enter them. Direct running of non-query methods with tags such as @INSERT, @DELETE is not supported.

The last

It’s safe to say that Database Inspector has greatly improved our Database experience, especially for Room. Don’t forget the conditions of use:

  • Android Studio 4.1 Canary 5 and above
  • Phones with API 26 and above

Another major update to Android Studio 4.1 is strong support for Jetpack Compose. As a perfect solution for data-driven UI, I will continue to improve.

Today’s article here, I have always insisted on the original bingxin said, more the latest Android dynamics, scan code to follow my public number!

This article was typeset using MDNICE