This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Data Persistence with Room!
You have completed Data Persistence with Room!
Preview
In this video we'll create the queries for the Topping table by using a Data Access Object!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
To start writing queries, we first need
to create the data access objects.
0:00
Let's start with creating the Dao for
our topping table.
0:05
In Android, we create a data
access object as an interface, so
0:08
inside our data package.
0:12
Let's create a new interface
called ToppingDao.
0:15
Make sure to pick Interface.
0:24
Then to designate it as
a data access object,
0:28
we just need to add the @Dao annotation.
0:31
And hit Enter to import it.
0:36
Inside the interface, we'll declare
each of our queries as a function
0:38
with an annotation to tell
room how to handle the query.
0:42
Let's see how it works by creating
a query to select all the toppings.
0:46
Let's add a function called getAll,
fun getAll and
0:51
let's make it return a list of toppings.
0:56
Remember since this is an interface,
we don't need to add a function body.
1:04
Then to tell room how
to get the results for
1:09
this function,
let's add a line above this one.
1:11
And add the @Query annotation and
some parentheses.
1:15
And inside the parentheses, we just
need to declare our query as a string.
1:21
So let's type select * from topping.
1:26
And there we go.
1:33
We've got a function that will return all
of the toppings and the topping table.
1:34
Also notice that we have syntax
highlighting in our SQL.
1:38
Thanks to room,
1:42
Android now has a few checks to make sure
queries are correct before we run the app.
1:43
It even knows what tables we have.
1:49
So if I accidentally change this toppin,
we'll get an error.
1:52
All right, let's move on to selecting
a specific topping by its ID.
1:57
Let's add a couple of lines
below our getAll function and
2:02
then create a new function
called getTopping, By Id,
2:05
then let's add an ID parameter as in Int,
and make sure it returns a topping.
2:10
Moving on to the query,
let's add the annotation.
2:19
And then add the query itself.
2:25
Let's go with select * from topping,
where id equals, and
2:28
this is where we need to
use our id parameter.
2:35
To reference a parameter in a query,
you just prefix it with a colon.
2:41
So let's add colon id.
2:46
And now we can get a specific
topping just from its id.
2:49
To finish up our ToppingDao,
all we need is an insert statement.
2:53
Represented as a function, it would
take in a topping and return nothing.
2:56
Let's add a couple lines at the bottom and
3:02
then declare a new insert function
to do just that, fun_insert.
3:04
And it will take in a topping.
3:10
And return nothing.
3:13
Then to tell room how
to handle the insert,
3:15
there's actually a special
@Insert annotation.
3:18
So all we need to do is add @Insert,
and we're done.
3:21
Great work finishing your
first data access object.
3:29
I know it might feel like a lot, but
3:31
one way to make sure you really understand
something is to get some practice.
3:33
Why don't you try to create
the other two dials on your own?
3:37
You can find the required queries below,
in the teacher's notes.
3:40
I'll show you how I do
it in the next video.
3:43
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up