Android, Java

Android greenDAO ORM For SQLite Example

Android Development

This post shows how to use greenDAO ORM for SQLite in Android using a demo app. We start with a simple Android project in the Android Studio, create a few entities, save to and read data from the database.

Requirements

This post’s requirements include Android Studio 4.1.2, Android API 30, Gradle 6.5, and Build Tools 30.0.2. The Android project is a simple project created using the Basic Activity Template.

Update [crayon-616b784728fe2541678252-i/] files – Both for Project And Module

For the project build.gradle, add the following highlighted line, which enables us to use the greenDAO plugin within Android Studio.

On the other hand, for the build.gradle for the module (or app), add the following two highlighted lines. The first line enables greenDAO to generate DAO files and update our entity classes for SQLite; the second line defines the greenDAO dependency for our Android app both for compile-time and runtime.

By the way, don’t forget to “Sync Now” the files to allow the changes to take effect.

Entities for greenDAO ORM

Then, we have the following entities – Person and Pet classes. The Person entity has a unique key constraint on both firstName and lastName fields. Meaning these fields must be unique for each row. The class also has an auto-incrementing @Id field and @NotNull fields.

The Pet entity has the following codes.

The entities have a bi-directional relationship with each other – one Person can have at least one Pet, and all Pet entities have references back to the Person entity!

Next, we need to rebuild the whole project via the Build > Make Project or Build > Rebuild Project menu option for GreenDAO ORM to update our entities and generate DAO classes, as shown below. Once we rebuilt the project, there are now four DAO-related classes, and our entities have been modified to have these new instance variables and methods.

Android greenDAO ORM For SQLite

 

Create Database via greenDAO ORM on Start-Up

There are two places where we can create the database for our Android app – either create the database in the onCreate() method of a subclass of Application or the main Activity class. The following codes create an SQLite database via greenDAO ORM on start-up.

When we start up our Android app, greenDAO ORM creates an SQLite database file “greendao-demo-db” if that file doesn’t exist yet in /data/data/com.turreta.greendaogeneraldemo/databases. If the file already exists, greenDAO ORM uses it.

Android greenDAO ORM For SQLite

Add, Edit, And Delete Entities in SQLite

From here on, we use an instance of DaoSession. Typically, we only have one instance of this class for the Android app’s life duration. Through that instance, we can access the DAO for each of our entity classes. Consider the following codes.

To create a Person record, we instantiate a Person object, set its properties, and pass it as an argument to its PersonDAO class’ save method. Note that we are using the same instance of DaoSession to get the appropriate DAO instance.

To update an entity, we first load it using the load method. Then, save the changes using the save() method. Another thing to note when using greenDAO, there is no cascade update of entities right now. We need to save or update each entity explicitly.

For deletion, greenDAO ORM doesn’t support cascade deletion as of this writing. Therefore, we need to delete records that hold foreign keys before deleting the records the keys refer to. For example, delete all Pet records before deleting the Person record that they refer to.

For further readings, please check out the greenDAO ORM documentation.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like