Hide chapters

Android Debugging by Tutorials

First Edition · Android 12 · Kotlin 1.6 · Android Studio Chipmunk (2021.2.1)

Section I: Debugging Basics

Section 1: 8 chapters
Show chapters Hide chapters

7. Debugging Databases
Written by Zahidur Rahman Faisal

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Data is a collection of facts, such as numbers, words, measurements, observations or just descriptions of things. - Math is Fun

The word Data originates from the word datum which means a single piece of information. Data is the plural for datum.

Basically, data is a distinct, small unit of information that can be used in various forms like text, numbers, media or bytes etc. It can be stored in electronic memory or pieces of paper just like in a book!

In the previous chapter, you learned about debugging views and optimizing layouts to display details of the podcast channel that you’ve subscribed. At this point, you must be wondering if you could debug the way podcasts are stored in PodPlay.

In this chapter, you’ll check how your subscribed podcasts are stored and organized in your database, and you’ll resolve the episode ordering issue.

In this process, you’ll learn how to:

  • Explore and update your existing data.
  • Modify your queries and watch live updates.
  • Export your Database.

Searching for The Data

Imagine a library that consists of many books where you can go and easily find a book about a topic you’re interested in or store a new book to read later. That’s your Database.

It wouldn’t be easy for you to find a book in a library if the books weren’t organized. You might get lost searching for a book that’s recently been added if someone just put the book randomly on any shelf. You might have faced the same situation in the PodPlay app! When using the app, you access a vast library of podcasts and can subscribe to your favorite ones to come back to later. The details of your subscribed podcast channels are stored locally in the app, just like a library. PodPlay uses a Room Database to store your subscriptions.

To start, open the Podplay starter project and run the app. Tap the search icon in PodPlay, type “RW” in the search bar and press Return. Choose any podcast and tap it to open its details. Finally, tap SUBSCRIBE to subscribe to the podcast.

You’ll now see the channel name in the subscribed channel list. Tap it to open a detailed screen, and you’ll see a list of episodes underneath the title and description of the channel. If you look closely, you may notice the episodes aren’t shown in the correct date order; you’d expect to see the recent episode on top of the list and then the older ones in chronological order.

There’s an integrated Database Inspector in Android Studio 4.1 and higher to assist your inspection of what’s gone wrong. The Database Inspector allows you to easily inspect, query, and modify the database in PodPlay, just like editing a spreadsheet; who wouldn’t want to take advantage of that!

The Database Inspector

You might be questioning how PodPlay’s database arranges your subscribed podcasts. Soon you’ll be able to see your table structure and schema using the Database Inspector. To open the Database Inspector in Android Studio, you need to:

Previewing Database

You’ll see a list of databases in your app and the tables each database contains within the Databases pane. The name of the database in PodPlay is PodPlayer.

Find the Schema

Here each column represents a field in your table. Expand the Podcast table to see how the schema was defined:

Updating Existing Data

Start by verifying if the data is in the correct order and getting stored in your table properly by following two simple steps:

Observing Live Updates

So far, it seems the data in the Podcast table has been stored and retrieved as expected. Now take it one step further, type something in the search bar, select a channel and tap SUBSCRIBE. It’s supposed to be on your Podcast table, but you can’t see any change!

Querying Your Databases

Since you verified that the subscribed podcasts are getting stored and retrieved correctly, the next step is to check if your database queries are correct.

Running DAO Queries

Data Access Object (DAO) is an interface that executes CRUD operations in your database, hiding the complexity behind it.

Running Custom Queries

Focus on the highlighted area in your query above; you’ll see your results in ascending order of each episode’s releaseDate. This must be why you don’t see the latest episode at the top of your list!

SELECT * FROM Episode WHERE podcastId = '1' ORDER BY releaseDate DESC

Exporting Data from the Database Inspector

The Database Inspector allows you to export databases, tables or query results. It’s a powerful feature to save, share or replicate your data. You can export using any of the following ways:

Key Points

  • You can look into your existing database easily with Database Inspector.
  • Check your database schema carefully and validate your assumptions about updating fields.
  • Using the Live updates feature will save you time to observe changes, but you can’t modify data in this mode.
  • Save time running your queries directly from the Android Studio DAO.
  • Copy and modify a query or write a custom one to see quick results.
  • Export your database for easy backup or sharing your data.

Where to Go From Here?

You’re now an expert on Database Debugging! The Database Inspector offers you other powerful features such as:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now