Sign in Go Pro

Storing Data in Android with Room

Reading data using the @Query Annotation

11/13/2018

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

Previous

About

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:

  • android.arch.persistence.room.Query 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

Links:

Instructor

Links

Comments

There are no comments on this lesson, start the conversation below ...

>
You need to go PRO to post comments.

Lessons in Storing Data in Android with Room