The benefit of apps that are handling non-trivial amounts of structured data is that user can carryon utilizing the app while it’s offline, because statistics is locally stored.
There is a Room persistence library that we can use to access data in SQLite.
Room provides the following benefits: :
There are three major components in Room:
Database
AppDatabase defines the database configuration and serves as the app’s main access point to the persisted data.
The class must be annotated with a @Database
The class must be an abstract class that extends RoomDatabase.
Define an abstract method that has zero arguments and returns an instance of the DAO class.
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract UserDao userDao();
}
You define each Room entity as a class that is annotated with @Entity.
A Room entity includes fields for each column in the corresponding table in the database, including one or more columns that comprise the primary key.
Each table has a primary key that can be set using @PrimaryKey annotation.
@Ignore annotation is to ignore fields.
full-text is for search for details in your database’s tables.
@AutoValue annotation can be used if there are two instances of an entity are equal.
Room uses the class name as the database table name,
tableName property of the @Entity annotation an @ColumnInfo annotation to set the name of the column.
In Room there are two ways to define and query a relationship between entities:
Relations:
@Relation annotation, parentColumn and entityColumn to set the relation between entities.
There are two types of DAO methods that define database interactions:
Convenience methods that let you @insert, @update, and @delete rows in your database.
Query methods that let you write your own SQL query to interact with the database.
Accessing data with Room ******