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
In this video we'll talk about what pieces we need to create a Room database, and we'll start creating one!
Code
implementation 'android.arch.persistence.room:runtime:1.1.1-rc1'
kapt 'android.arch.persistence.room:compiler:1.1.1-rc1'
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
[MUSIC]
0:00
[SOUND] We've solved the first problem.
0:03
Let's move on to the second one.
0:06
We need to implement a way for
our users to save their pizzas.
0:08
To do this,
0:12
we'll be using the Room library to store
pizza information in a SQLite database.
0:12
Room makes maintaining a database
a lot easier than it used to be.
0:17
Instead of having to write all the SQL
yourself, Room gives us a set of
0:21
components that handle a lot of
that work behind the scenes.
0:25
Let's take a minute to look
at these components and
0:28
see how it all fits together.
0:31
The first component of a RoomDatabase is
an entity, which represents a table and
0:32
the database.
0:37
Stepping up from an entity, we have data
access objects, also referred to as DAOs.
0:38
A data access object is where we'll
keep all the queries we need for
0:44
a specific table.
0:47
At the very top is the RoomDatabase,
which groups together all of the DOAs and
0:49
entities and provides a single
point of entry from within the app.
0:54
We'll talk more about each
of those in just a bit.
0:58
But before we can start on anything,
1:00
we'll need to decide on
the structure of our database.
1:03
What tables does it have?
1:05
What columns are in those tables?
1:07
And what relationships do we need
to maintain between columns?
1:08
For us, we'll have a Pizza table and
a Toppings table.
1:13
Both with id primary keys.
1:16
But while this lets us
store some information,
1:19
it's not giving us the whole story.
1:21
We're missing out on which
toppings belong on which pizza.
1:23
To get this information,
we'll need to add a third table
1:27
to keep track of all the pizza and
topping combinations.
1:29
This PizzaTopping table will
have two columns which are both
1:33
foreign keys to other tables.
1:36
And together, those two columns
also make up the primary key.
1:38
If you'd like to read more about
why we need the PizzaTopping table,
1:42
check out the article below on how
to handle a many-many relationship.
1:47
Okay, now that we've got a better
idea of what we have to do,
1:51
let's start building up from the bottom
and get to work on those entities.
1:54
Heading back to Android Studio,
1:58
I'll clean up my workspace a little
bit by closing all of these tabs.
2:00
Then the first thing we need to do is
add a couple import statements for
2:04
the Room library.
2:08
Let's head over to our apps
build.gradle file and copy and
2:10
paste in these two lines to the bottom
of the dependencies section.
2:13
Also, they've been updating these
libraries somewhat frequently.
2:18
So if you see a newer version warning,
2:22
feel free to use Alt+Enter to
upgrade to the newest version.
2:24
Then let's sync the project, And
now we can go about using our entities.
2:28
Let's head over to the Pizza class and
add a line above the class declaration.
2:35
And then type @Entity and
hit Enter to import it.
2:42
And there we go,
our Pizza class is now a Pizza entity.
2:46
However, all entities
require a primary key.
2:51
So we're not quite done yet.
2:55
To tell Room which column
is the primary key,
2:57
we just need to add the @PrimaryKey
annotation before that column.
3:00
Let's add a line above id and
then type, @PrimaryKey.
3:04
You may need to use
Alt+Enter to import it.
3:14
Awesome, that's it for our Pizza entity.
3:17
Now let's update the Topping class.
3:20
Let's add the @ entity annotation,
and then the @ primary key annotation.
3:24
You could also put it on the same
line as the id if you prefer.
3:32
Now that we've got Pizza and
Topping entities,
3:37
it's time to create the PizzaTopping
entity, which we'll do in the next video.
3:40
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