Start a Project

Android Room Persistence Library

The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Add the Google Maven repository:

Add this dependencies in module build.gradle:

Now let’s begin with Room persistence library create a class User. User Class is annotated with @Entity and name of the table. To make field primary key, you need to annotate a field with @PrimaryKey and property autoGenerate which assign automatic IDs.

Create a data access object using an interface. This class is annotated with @Dao annotation. Room will generate an implementation of defined methods. There are four annotations @Query@Insert.

Create a database holder called AppDatabase extends RoomDatabase, we will define list of entities and database version. Class is annotated with @Database annotation. It is good practice to use singleton approach for the database, so you need to create an static method which will return instance of AppDatabase.

For insert data in database and get User list from the data:

 

Exit mobile version