How to Use Realm Database in Android? CURD Operation.

Daya Nithi
3 min readDec 26, 2019

--

The Most Popular database for android. Everyone will be using sq-lite or Room database to store data. In-fact You should try this too.

How to Add it to your android Project?

Add this line to Your Project level Dependency.

Add this to Your App level Dependency. Then Sync Your Project. That’s all…You are all set to start the engine. Come on Lets get our hands dirty.

Here In Realm We can Create Update Read and Delete easily with all inbound Functions.

Setup your db in Application class or Any common class.

Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().name("default.realm").build();
Realm.setDefaultConfiguration(config);

This will create the database with all default configuration. Dont change the name “default.realm”.

Create and Insert.

In Realm all data/Table are made up of models. You can create n number of models i.e Tables.

Eg:

public class Parameter extends RealmObject {


private int id;
private Date DateTime;
private String Data;
private String Status;


public int getId() {
return id;
}

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

public Date getDateTime() {
return DateTime;
}

public void setDateTime(Date datetime) {
this.DateTime = datetime;
}

public String getData() {
return Data;
}

public void setData(String data) {
this.Data = data;
}

public String getStatus() {
return Status;
}

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

}

This Class is a Model class. But this is how we will create our Table in Realm. All class should extend RealmObject.

Create and Insert and Update.

Realm myRealm = Realm.getDefaultInstance();
myRealm.beginTransaction();
Parameter parameter = myRealm.createObject(Parameter.class);
parameter.setId(Id);
parameter.setData("Hello");
parameter.setStatus("create");
parameter.setDateTime(Utils.getCurrentDateWithoutTime());
myRealm.insertOrUpdate(parameter);


myRealm.commitTransaction();
myRealm.close();

All values should be encapsulated by Begin Transaction and End Transaction.Finally You can close the realm Instance.

Hurrah!!! we have created the table and inserted into the realm Database.

The same code will be applicable for update. InsertOrUpdate Function works on this. Simple as possible.

Read all values in table

Realm myRealm = Realm.getDefaultInstance();
myRealm.beginTransaction();
RealmResults<Parameter> parameters = myRealm.where(Parameter.class).equalTo("id", Id).findAll();
myRealm.commitTransaction();
myRealm.close();

Now You have read all values inside the Parameter Table as List.

Parameter parameter = myRealm.where(Parameter.class).findFirst();

This gives you the first Object of Parameter Table.

Easily Sort the Date Object.

RealmResults<Parameter> realmResults1 = myRealm.where(Parameter.class).distinct("dateTime").notEqualTo("dateTime", Utils.getCurrentDateWithoutTime()).sort("dateTime", Sort.DESCENDING).findAll();

Similarly you can sort with Id also which is a Integer Type.

How to Add Realm Results to ArrayList?

public static ArrayList<PingInfo> Search_Ping_Info_Status(String s) {
ArrayList<Parameter> parameterArrayList = new ArrayList<>();
Realm myRealm = Realm.getDefaultInstance();
RealmResults<Parameter> results = myRealm.where(Parameter.class).equalTo("Status", s).findAll();
myRealm.beginTransaction();
parameterArrayList.addAll(myRealm.copyFromRealm(results));
myRealm.commitTransaction();
myRealm.close();
return parameterArrayList;



}

Thus CopyfromRealm adds all values to the arraylist.

Delete From Realm:

RealmResults<Parameter> totaldates = myRealm.where(Parameter.class).lessThan("DateTime", realmResults.get(30).getDateTime()).findAll();
totaldates.deleteAllFromRealm();

This Piece of code deletes all from realm.

If You want a particular data just read that id and add it to delete functions.

That’s It we have learned today All Curd Operations in Realm and You guys try to append in your projects. Lets meet in Next Tutorial.

--

--

Daya Nithi

6+ years of development of android applications. Exploring Flutter and more to come.