Droid - Database - Room
Droid - Database - Room
• The most common use case is to cache relevant pieces of data so that when the device cannot access the network, the user can still browse
that content while they are offline.
• The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of
SQLite.
1 The database class that holds the database and serves as the main
access point for the underlying connection to your app's persisted
data.
3 Data access objects (DAOs) that provide methods that your app
can use to query, update, insert, and delete data in the database.
Data entity
The following code defines a Student data entity.
Each instance of Student represents a row in a user table in the app's database.
Data access object (DAO)
The following code defines a DAO called StudentDao.
StudentDao provides the methods that the rest of the app uses to interact with
data in the user table.
Database
The following code defines an StudentDatabase class to hold the database.
1 The class must be annotated with a @Database annotation that includes an entities
array that lists all of the data entities associated with the database.
3 For each DAO class that is associated with the database, the database class must define
an abstract method that has zero arguments and returns an instance of the DAO
class..
Usage
After you have defined the data entity, the DAO, and the database object, you can use the following code to create an
instance of the database:
You can then use the abstract methods from the StudentDatabase to get an instance of the DAO.
In turn, you can use the methods from the DAO instance to interact with the database:
https://developer.android.com/training/data-storage/room
https://developer.android.com/training/data-storage/room/accessing-data