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 Daos for our Pizza and PizzaTopping tables!
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
Did you figure it out?
0:00
No worries if you didn't,
0:02
we'll work through it anyway
starting with the PizzaDao.
0:03
First, we create a new interface and
the data package.
0:06
Named PizzaDao.
0:12
Then we make it official by
adding the @Dao annotation
0:16
Inside the Dao, we'll create a query
to select all the pizzas, @Query.
0:24
Select * from pizza.
0:32
Then we'll declare a function
called getAll to go with our query.
0:36
And, it'll return all list of pizzas.
0:43
Moving on to selecting a specific pizza,
we'll create the query.
0:47
Which is select * from
0:54
pizza where id = :id.
0:58
Then we're at the function,
which is fun getPizzaById with
1:03
an id parameter that
returns a pizza object.
1:11
Next we've got the Insert statement.
1:15
So let's add the Insert annotation.
1:18
Along with the insert function,
fun insert and it should take in a pizza.
1:21
Finally, we need a query to
delete a pizza based on an ID.
1:27
There is actually an @Delete annotation,
but it only works with pizza objects.
1:32
So if we want to delete something by
an ID, we'll have to use a query.
1:38
Let's add a query annotation.
1:43
And then for the query itself,
1:46
let's go with delete from
pizza where id = :id.
1:51
Then let's add the function,
which should take a none Id and
1:58
return nothing, fun delete pizza
2:03
by Id which takes a none Id and
returns nothing, awesome!
2:08
Now, let's move on to
the pizza toppingDao.
2:14
Let's create another new interface.
2:17
Called PizzaToppingDao.
2:21
Then let's add the @Dao annotation.
2:27
And let's add a query to give us which
topping IDs go with a certain pizza ID.
2:31
@Query, and then select toppingId from
2:35
pizzatopping where pizzaId = :id.
2:43
And let's also create the function for
that.
2:50
Fun ToppingIdsForPizzaId, and
2:55
we'll pass in an ID which is an integer.
3:00
And we'll be returning a list of topping
IDs, which is a list of integers.
3:09
Moving on to the insert statement,
let's add the @Insert annotation followed
3:16
by the insert function, fun insert and
it takes in the pizzaTopping object.
3:22
And last but not least,
3:29
what's @Query to remove rose
associated with a specific pizza ID.
3:31
Let's start with a query notation and
then the query,
3:37
delete from pizzatopping
where pizzaId = :id.
3:42
Then adding the function,
fun deletePizzaById
3:48
which takes an (id:int) and
returns nothing.
3:53
Alright, that wraps up creating our Daos.
3:59
Let's think back to our data model and
see what we need to make next.
4:02
We've got the entities and
we've got the data access objects.
4:06
So it must be time to create the room
database to tie it all together.
4:10
We'll do that in the next video.
4:14
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