Updated 2 January 2018
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:
1 2 3 4 5 6 |
allprojects { repositories { jcenter() google() } } |
Add this dependencies in module build.gradle:
1 2 |
compile 'android.arch.persistence.room:runtime:' + rootProject.archRoomVersion; annotationProcessor 'android.arch.persistence.room:compiler:' + rootProject.archRoomVersion; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@Entity(tableName = "user") public class User { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "first_name") private String firstName; @ColumnInfo(name = "last_name") private String lastName; @ColumnInfo(name = "age") private int age; public int getId() { return uid; } public void setId(int uid) { this.uid = uid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
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.
1 2 3 4 5 6 7 8 9 10 |
@Dao public interface UserDao { @Query("SELECT * FROM user") List<User> getAll(); @Insert void insertAll(User... users); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Database(entities = {User.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { private static AppDatabase mINSTANCE; public abstract UserDao userDao(); public static AppDatabase getAppDatabase(Context context) { if (mINSTANCE == null) { mINSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "demo-database") .allowMainThreadQueries() .build(); } return mINSTANCE; } public static void destroyInstance() { mINSTANCE = null; } } |
For insert data in database and get User list from the data:
1 2 3 4 5 6 7 8 9 10 |
User user = new User(); user.setFirstName("Ajay"); user.setLastName("Saini"); user.setAge(25); // Get instance of AppDatabase AppDatabase db=AppDatabase.getAppDatabase() // Insert data into Database db.userDao().insertAll(user); // Get the list of User from the Database List<User> user=db.userDao().getAll(); |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.