Sign in Go Pro

Storing Data in Android with Room

Reading data using the @Query Annotation

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson

Up next



In this lesson, we'll describe the @Query annotation and see how it can be used to read data from the Room database.

We'l see how Room's compile-time query checking works to ensure that the SQL you write is valid from the moment you write it.

We demonstrate how to write a function in the DAO which returns all of the Tasks currently in the database using the @Query annotation.

For this lesson, we manually call the DAO to obtain tasks from the onCreate method and after we've inserted a Task into the DB. This pattern of manually calling the refresh function isn't ideal and we'll go on to highlight a better way of doing this using LiveData in another lesson. But for now, this will teach the basics of how to invoke a query defined using the @Query annotation.

Summary of Content:

  • Annotation
  • Room provides compile-time query checking for @Query
  • Room provides syntax highlighting on the query
  • Not just checking syntax; Room actually checks the query against the database schema you have defined
  • Room also checks the return value specified to ensure what is being returned from the SQL query can be mapped to the specified return type
  • How to read the DB from a background thread but update the UI from the main thread





You are not explaining TaskListAdapter which is required at this point but not mentioned in any of the previous modules?


Lessons in Storing Data in Android with Room