Skip to content
pavlo-savchuk edited this page May 18, 2016 · 5 revisions
  • DatabaseHelper
  • DatabaseFacade
  • StubItemDAO

###DatabaseHelper public class DatabaseHelper extends SQLiteOpenHelper

For managing all the operations related to the database , an helper class has been given and is called SQLiteOpenHelper. To use SQLiteOpenHelper, create a subclass that overrides the onCreate(), onUpgrade() and onOpen() callback methods. The onUpgrade() method will simply delete all existing data and re-create the table.

For example, here's an implementation of SQLiteOpenHelper that uses some of the commands shown above:

public class DatabaseHelper extends SQLiteOpenHelper {

   private static final String DATABASE_NAME = "main_db";

   private static final int DATABASE_VERSION = 1;

   private final Resources appResources;

   DatabaseHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
       this.appResources = context.getResources();
   }

   private static List<String> getInitDatabaseQueries(final Resources theAppResources) {
       final List<String> queryList = new ArrayList<String>();

       queryList.add(theAppResources.getString(R.string.create_table_stub_items));
       //TODO: add other tables here

       return queryList;
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
       Iterator<String> it = getInitDatabaseQueries(this.appResources).iterator();
       while (it.hasNext()) {
           String subQuery = it.next();
           db.execSQL(subQuery);
       }
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       //TODO: handle data migration here
   }

}

Note All queries to create set of tables are placed in string resources

###DatabaseFacade public static class DatabaseFacade

DatabaseFacade is a singleton that wraps DatabaseHelper and SQLiteDatabase.

It is used to:

  • open/close a database
  • check if database contains any records
  • create rawQuery
  • save/update/delete database table
  • get all records
  • clear table
  • StubItemDAO

###public class StubItemDAO extends AbstractDAO

Data access object (DAO) will be used to manage the data. The DAO is responsible for handling the database connection and for accessing and modifying the data. It will also fetch value objects from database.

DAO allows the following methods:

  • get/update/save object from database.
  • read/remove Items for page Id from database.

The following figure shows retrieve data from database:

Diagram 3

Clone this wiki locally