Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
A RoomDatabase ties together Entities and Daos to create one cohesive database. In this video we'll create the RoomDatabase for our app!
Related Links
Now that we've created our dows and
0:00
our entities it's time to create a room
database to help organize all of it.
0:02
As per usual this will
involve using annotations.
0:07
And it'll even involve
using an abstract class.
0:11
But don't worry,
0:14
you don't need to understand how it works
under the hood to be able to use it.
0:15
The first thing we'll need to
do is create an abstract class
0:19
that extends from room database.
0:22
Inside the data package, let's create
a new class, And name it pizza database.
0:25
Then let's make this an abstract class
by adding abstract to the front and
0:34
make it extend from room database.
0:39
And use Alt+Enter to add
the call to the constructor.
0:44
Next, above the class,
we need to add the @Database annotation.
0:47
Inside this annotation is
where it'll specify which
0:52
entities are in our database,
as well as the version number.
0:54
We'll start the version number at 1, and
0:58
increase it every time we change
something structural about our database.
1:00
Like adding a new table or
renaming a column.
1:04
Inside the parenthesis,
1:07
let's set the entities parameter equal to
an array containing our entity classes.
1:09
We've got Pizza::class,
1:14
PizzaTopping class and
1:19
Topping::class.
1:24
Then let's add a comma, and
set version equal to 1.
1:28
Awesome, now that we've connected
the entities to our database,
1:33
it's time to give access to our DAOs.
1:38
For each DAO,
1:40
we'll need to add an abstract function
that returns an instance of that DAO.
1:41
Let's start by creating
an abstract function PizzaDao and
1:46
make it return a PizzaDao.
1:51
>> Then let's make another abstract
function called PizzaToppingDao.
1:56
>> And make it return a PizzaToppingDao.
2:03
>> And finishing up,
let's create an abstract function
2:07
called toppingDao which
returns toppingDao.
2:12
All right, that does it for
our room database.
2:15
Let's see how to use it.
2:20
Let's open up main activity.
2:21
And right below the call to set content
view, let's create a new val named db and
2:24
set it equal to room inner to
import it database builder.
2:31
And then we need to pass in a context,
2:38
the Java class of our room database and
what we would like to call the database.
2:40
So let's pass in this for the context.
2:47
PizzaDatabase::class.java for the class.
2:49
And let's call it PizzaDatabase.
2:56
And then give us a little
more room as well.
3:04
And now we've got our
database builder object.
3:08
From here we can set some
properties of our database or
3:11
use the build a function to return
an instance of our specific database.
3:15
Which in our case, is a pizza database.
3:19
Let's add a call to .build which we
can see returns a pizza database.
3:22
And on the next line, if we type db.,
we've got access to each
3:27
of our DAOs and
can call in to any of our queries.
3:32
Okay, it's time to start
implementing this in the app and
3:39
finally make the UI do something, right?
3:43
Not quite, we've done a lot of work here.
3:46
But to make sure we've done it correctly,
we'll need to test it.
3:49
Sure, we could just test it in the app,
but then we'd have to go through and
3:52
test everything manually.
3:57
And that might be hard to tell the
difference between a bug in the database
3:58
and a bug in the UI.
4:02
Instead, let's write some tests
to make sure everything's working
4:03
before we start putting it in the app.
4:07
You need to sign up for Treehouse in order to download course files.
Sign up