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