Android OrmLite over SQLite example

suggest change

ORMLite is an Object Relational Mapping package that provides simple and lightweight functionality for persisting Java objects to SQL databases while avoiding the complexity and overhead of more standard ORM packages.

Speaking for Android, OrmLite is implemented over the out-of-the-box supported database, SQLite. It makes direct calls to the API to access SQLite.

Gradle setup

Add the library to the module build.gradle:

// https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-android
compile group: 'com.j256.ormlite', name: 'ormlite-android', version: '5.0'

POJO configuration

Configure each POJO that should be stored. ORMLite will not see the fields unless they are annotated:

@DatabaseTable(tableName = "form_model")
public class FormModel implements Serializable {

   @DatabaseField(generatedId = true)
   private Long id;
   @DatabaseField(dataType = DataType.SERIALIZABLE)
   ArrayList<ReviewItem> reviewItems;

   @DatabaseField(index = true)
   private String username;

   @DatabaseField
   private String createdAt;

   public FormModel() {
   }

   public FormModel(ArrayList<ReviewItem> reviewItems, String username, String createdAt) {
       this.reviewItems = reviewItems;
       this.username = username;
       this.createdAt = createdAt;
   }
}

The example above is one table (form_model) with four fields.

id field is auto generated index.

username is an index to the database.

More information about the annotation can be found at the official documentation.

Database Helper

To continue with, you will need to create a database helper class which should extend the OrmLiteSqliteOpenHelper class.

This class creates and upgrades the database when your application is installed and can also provide the DAO classes used by your other classes.

DAO stands for Data Access Object. It provides the CRUD operations for one persisted class.

The helper class must implement the following two methods:

onCreate creates the database when your app is first installed

onUpgrade handles the upgrading of the database tables when you upgrade your app to a new version

Database Helper class example:

public class OrmLite extends OrmLiteSqliteOpenHelper {
  
      //Database name
      private static final String DATABASE_NAME = "gaia";
      // Changing DATABASE_VERSION calls onUpgrade.
      private static final int DATABASE_VERSION = 2;
  
      /**
       * The data access object used to interact with the Sqlite database to do C.R.U.D operations.
       */
      private Dao<FormModel, Long> todoDao;
  
  
  
      public OrmLite(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION,
                  /**
                   * R.raw.ormlite_config is a reference to the ormlite_config2.txt file in the
                   * /res/raw/ directory of this project
                   * */
                  R.raw.ormlite_config2);
      }
  
      @Override
      public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
          try {
  
              /**
               * creates the database table
               */
              TableUtils.createTable(connectionSource, FormModel.class);
  
          } catch (SQLException e) {
              e.printStackTrace();
          } catch (java.sql.SQLException e) {
              e.printStackTrace();
          }
      }
      /*
          It is called when you construct a SQLiteOpenHelper with version newer than the version of the opened database.
       */
      @Override
      public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource,
                            int oldVersion, int newVersion) {
          try {
              /**
               * Recreates the database when onUpgrade is called by the framework
               */
              TableUtils.dropTable(connectionSource, FormModel.class, false);
              onCreate(database, connectionSource);
  
          } catch (SQLException | java.sql.SQLException e) {
              e.printStackTrace();
          }
      }
  
      /**
       * Returns an instance of the data access object
       * @return
       * @throws SQLException
       */
      public Dao<FormModel, Long> getDao() throws SQLException {
          if(todoDao == null) {
              try {
                  todoDao = getDao(FormModel.class);
              } catch (java.sql.SQLException e) {
                  e.printStackTrace();
              }
          }
          return todoDao;
      }
  }

Persisting Object to SQLite

Finally, the class that persists the object to the database.

public class ReviewPresenter {
private GaiaApplication application;
Dao<FormModel, Long> simpleDao;


public ReviewPresenter(Application application) {
   this.application = (GaiaApplication) application;
   simpleDao = this.application.getHelper().getDao();
}

public void storeFormToSqLite(FormModel form) {

   try {
       simpleDao.create(form);
   } catch (SQLException e) {
       e.printStackTrace();
   }
   List<FormModel> list = null;
   try {
// query for all of the data objects in the database
       list = simpleDao.queryForAll();
   } catch (SQLException e) {
       e.printStackTrace();
   }
// our string builder for building the content-view
   StringBuilder sb = new StringBuilder();
   int simpleC = 1;
   for (FormModel simple : list) {
       sb.append('#').append(simpleC).append(": ").append(simple.getUsername()).append('\n');
       simpleC++;
   }
   System.out.println(sb.toString());
}

//Query to database to get all forms by username
public List<FormModel> getAllFormsByUsername(String username) {
   List<FormModel> results = null;
   try {
       results = simpleDao.queryBuilder().where().eq("username", username).query();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return results;
}
}

The DAO comes from the application helper, defined as:

private OrmLite dbHelper = null;

/*
Provides the SQLite Helper Object among the application
*/
public OrmLite getHelper() {
   if (dbHelper == null) {
       dbHelper = OpenHelperManager.getHelper(this, OrmLite.class);
   }
   return dbHelper;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents