Android room persistence database tutorial

Aneh Thakur
TrinityTuts
Published in
2 min readJan 18, 2020

Android room persistence library is a layer upon the SQLite database to give us the most robust access to the database. The room persistence library is also a part of Android Architecture Components. With the help of Room library, we have fluent database access while harnessing the full power of SQLite.

Before we start you need to add a library in your project

implementation "androidx.room:room-runtime:2.2.3"
annotationProcessor "androidx.room:room-compiler:2.2.3"

Creating an Entity class for Room

@Entity(tableName = "users")
public class UserTable {


@PrimaryKey(autoGenerate = true)
int id;

@ColumnInfo(name = "name")
String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

@ColumnInfo(name = "status")
String status;
}

Creating a DAO interface

@Dao
public interface UserQueryDao {

@Query("SELECT * FROM users")
LiveData<List<UserTable>> fetchAllUsers();


@Query("SELECT * FROM users")
List<UserTable> getAllUsersList();

@Insert
Long insertTask(UserTable user);

@Query("SELECT id, name, status FROM users WHERE id=:id")
LiveData<UserTable> getUser(int id);

@Query("DELETE FROM users WHERE id=:id")
void deleteSpecific(int id);

@Query("DELETE FROM users WHERE 1=1")
void deleteAll();

@Update
void updateUsers(UserTable userTable);
}

Creating a Database class

@Database(entities = {UserTable.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract UserQueryDao queryDao();
}

Creatin action class to perform CURD

public class UserActions {

private String DB_NAME = "users";

private AppDatabase userDB;

public UserActions(Context context) {
userDB = Room.databaseBuilder(context, AppDatabase.class, DB_NAME).build();
}

public LiveData<List<UserTable>> getLiveUsers() {
return userDB.queryDao().fetchAllUsers();
}

public List<UserTable> getAllUsers() throws ExecutionException, InterruptedException {
return new getAllAsyncTask(userDB).execute().get();
}

private static class getAllAsyncTask extends android.os.AsyncTask<Void, Void, List<UserTable>> {

private AppDatabase userDB;
List<UserTable> a;

getAllAsyncTask(AppDatabase dao) {
userDB = dao;
}

@Override
protected List<UserTable> doInBackground(Void... voids) {
return userDB.queryDao().getAllUsersList();
}
}


public void insertTask(String name, String status) {

UserTable user = new UserTable();
user.setName(name);
user.setStatus(status);

insert(user);
}

public void insert(final UserTable user) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
userDB.queryDao().insertTask(user);
return null;
}
}.execute();
}

public void delete(final int id) {
final LiveData<UserTable> user = getUser(id);
if (user != null) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
userDB.queryDao().deleteSpecific(id);
return null;
}
}.execute();
}
}

public void update(final UserTable userData) {
final LiveData<UserTable> user = getUser(userData.getId());
if (user != null) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
userDB.queryDao().updateUsers(userData);
return null;
}
}.execute();
}
}

public void deleteAll() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
userDB.queryDao().deleteAll();
return null;
}
}.execute();
}

public LiveData<UserTable> getUser(int id) {
return userDB.queryDao().getUser(id);
}
}

You can call your Room operation like this

UserActions userActions = new UserActions(getApplicationContext());
userActions.insertTask("Aneh Thakur", "Welcome to trinitytuts");

You can read complete implementation from this blog post:- Working with Android room persistence database library.

--

--