Wednesday, June 12, 2013

ORM in android

Pre-req: Eclipse, android, Android Clients,

Hi guys,

Dealing with database in android takes alot of time.

  • First create schema i.e database and tables
  • Create database and tables with Database helper
  • Populate database i.e tables from android components i.e activities, services
  • Create queries, query manger classes and populate query results into Object

 Any mistakes in above step case crashes. We can speed up this process with some ORM, In this tutorial we will create a database with OrmLite and will see how many steps in sqlite database management we can skip. This is a basic level tutorial.


1) Create POJO/Class(s) these classes are tables


@DatabaseField(generatedId = true) //this annotation shows auto generated id column 
@DatabaseField(index = true) // column with index on it 
@DatabaseField(canBeNull = false, foreign = true) // a foreign key column, with not null constraint
@ForeignCollectionField(eager = false) //one to many relation
2)Create a Database Helper
  We always create database helper to create/update database, you just need to 
copy past database helper which is extension of OrmLiteSqliteOpenHelper.
 
Only with this command we can create a table.
TableUtils.createTable(connectionSource, SimpleData.class);
3) Create DAOs inside database helper:
public Dao<SimpleData, Integer> getSimpleDataDao() throws SQLException {
          if (simpleDao == null) {
                    simpleDao = getDao(SimpleData.class);
          }
         return simpleDao;
}
This DAO object help you to create rows, process query.

4) How to use in activity:
private DatabaseHelper getHelper() {
      if (databaseHelper == null) {
                databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
      }
      return databaseHelper;
 }
Queries data: 
Dao<SimpleData, Integer> simpleDao = getHelper().getSimpleDataDao();
long millis = System.currentTimeMillis(); 
// create some entries in the onCreate
SimpleData simple = new SimpleData(millis); 
simpleDao.create(simple);// Create row
         simpleDao.delete(simple);// delete row

         simpleDao.update(simple);//update row
List<SimpleData> list = simpleDao.queryForAll(); //query all object
         Query:
QueryBuilder<SimpleData, Integer> queryBuilder = simpleDao.queryBuilder();// get the 
//WHERE object to build our query
Where<SimpleData, Integer> where = queryBuilder.where(); 
// the name field must be equal to "foo"
where.eq("string", "foo"); // and operation
where.and();
// the password field must be equal to "_secret"
where.eq("id", "1"); 
PreparedQuery<SimpleData> preparedQuery = queryBuilder.prepare(); 
SimpleData simpleData = (SimpleData) simpleDao.query(preparedQuery);

Conclusion: 
  • We do not need to create sql schema for tables and database, table records management classes/providers.
  • We just need 2 thing , models (POJO) and extension of OrmLiteSqliteOpenHelper for conversion of models (POJO) into tables.
  • We access database from any android component i.e. activities , fragments, services.
  • No need to map results rows to object separately. For insertion and updation just save whole objects.
  • You can use same objects to map json data, if require.
  • It is simple to configure and use

Reference

Source code



No comments:

Post a Comment