Chapters

Hide chapters

Saving Data on Android

Second Edition · Android 11 · Kotlin 1.5 · Android Studio 4.2

Using Firebase

Section 3: 11 chapters
Show chapters Hide chapters

9. The DAO Pattern
Written by Subhrajyoti Sen

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

In the previous chapter, you learned about different kinds of relations, such as one-to-one, one-to-many and many-to-many. You also learned how to create them using annotations.

In this chapter, you’ll learn how to retrieve, insert, delete and update data from your database using Database Access Objects (DAO).

Along the way, you will also learn:

  • What DAOs are and how they work.
  • How to create DAOs using Room annotations.
  • How to prepopulate the database using a provider class.
  • How to perform INSERT INTO queries using @Insert annotated methods.
  • How to perform DELETE FROM queries using @Deleteannotated methods.
  • How to use @Query to read data from the database.

Ready? Dive in!

Getting started

Download the starter project attached to this chapter and open it using Android Studio 4.2 or above. Once Gradle finishes building your project, take some time to familiarize yourself with the code. If you have been following along, to this point, you should already be familiar with the project since it is the same as the final project from the last chapter. If you are just getting started, here is a quick recap of the code:

  • The data package contains two packages: db and model. The db package contains the QuestionDatabase class, which defines your Room database. The model package contains your entities: Question and Answer.
  • The view package contains all your activities: SplashActivity, MainActivity, QuestionActivity, and ResultActivity.

Now, build and run the app to verify that everything is working properly.

The Main screen.
The Main screen.

Cool! Now you are ready to start creating some Database Access Objects to manipulate the data.

Using DAOs to query your data

Database Access Objects are commonly known as DAOs. DAOs are objects that provide access to your app’s data, and they are what make Room so powerful since they abstract most of the complexity of communicating to the actual database. Using DAOs instead of query builders or direct queries makes it very easy to interact with your database. You avoid all the hardship of debugging query builders, if something breaks, and we all know how tricky SQL can be! They also provide a better separation of concerns to create a more structured application and improve its testability.

Creating a provider class

Now that your DAO methods are ready, you’ll create a provider class, that you will need later on, to prepopulate your database. Create a new class under the data package, name it QuestionInfoProvider and add the following method:

Kacuzyaxh ylaw xve iqq ut hovweky bugo.
Dawapkotf hfex mfa ejq en qenkozw mape.

Testing your database

Although your app’s UI isn’t working yet, you can still interact with your database by performing some tests such as adding or deleting questions to verify its functionality.

Jye efslaunNutt tetcofo.
Wte apwpeetRivn qektolu.

Hupfemb rko Ofsfbedumkxaed Lamk.
Luknicn kxe Ednktofolqhooh Firx.

Key points

Where to go from here?

You now know how to create DAOs to interact with your database. You can download the final project by opening the attachment on this chapter, and if you want to learn more about DAOs in Room, you can explore the following resources:

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.
© 2026 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