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

To get started you should include the package to the build gradle.

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

Then you should configure a POJO to be persisted to the database. Here care must be taken to the annotations:

@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;
   }
}

At the example above there is one table (form_model) with 4 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 and it provides all the scrum functionality and specializes in the handling a single 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";
      //Version of the database. Changing the version will call {@Link OrmLite.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 {
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", PreferencesManager.getInstance().getString(Constants.USERNAME)).query();
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return results;
}
}

The accessor of the DOA at the constructor of the above class is 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:


ORMLite:
* Android OrmLite over SQLite example

Table Of Contents
2 Gradle
5 Intent
17 Service
19 WebView
31 SQLite
35 Glide
37 Dialog
38 ACRA
44 Handler
53 Toast
63 Menu
65 Picasso
70 Volley
71 Widgets
78 Realm
90 Spinner
95 OkHttp
108 TextView
109 ListView
111 Loader
118 Xposed
119 Security
121 ImageView
123 Doze Mode
130 Drawables
131 Colors
134 Fresco
139 AdMob
145 Keyboard
146 Button
150 EditText
155 Vk SDK
163 ExoPlayer
169 XMPP
175 OpenCV
177 Threads
184 ORMLite
186 TabLayout
190 LruCache
192 Zip files
194 Fastlane
199 FileIO
202 Moshi
210 VideoView
216 Paint
218 ProGuard
226 CleverTap
228 ADB shell
229 Ping ICMP
230 AIDL
234 Context
240 JCodec
242 Okio
249 FuseView
254 Looper
261 Fastjson
263 Jackson
267 Smartcard