Exploring the Database Inspector in Android Studio

The long-awaited tool for Android developers is here

Image for post
Image for post
Photo by Dennis Kummer on Unsplash.

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.

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).

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.

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
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
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:

Image for post
Image for post
Executing Dao queries

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:

Image for post
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.

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.

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:

Image for post
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
Image for post
Enable live updates in database inspector

Conclusion

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

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanks to Zack Shapiro

Siva Ganesh Kantamani

Written by

Learn. Code. Write. Repeat. Visit me at https://about.me/sivaganesh_kantamani & Join my email list at https://tinyletter.com/Siva_Ganesh_Kantamani

Better Programming

Advice for programmers.

Siva Ganesh Kantamani

Written by

Learn. Code. Write. Repeat. Visit me at https://about.me/sivaganesh_kantamani & Join my email list at https://tinyletter.com/Siva_Ganesh_Kantamani

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store