0

I am trying to work out how best to implement an SQLite database in my Android app. Advice from this answer suggests using SQLiteOPenHelper, which I already have, but recommends only having 1 instance of it.

In my app I have an abstract BaseDAO class which extends SQLiteOpenHelper and creates the database tables if necessary. E.g:

public abstract class BaseDAO extends SQLiteOpenHelper{


    public BaseDAO(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE " + ...
        db.execSQL(CREATE_TABLE);
    }

I have other DAO classes set up to access the database, which all extend this BaseDAO class, e.g. a MessageDAO class for putting message objects into the db.

public class MessageDAO extends BaseDAO {

    public MessageDAO(Context context) {
        super(context, DBContract.DB_NAME, null, DBContract.DB_VERSION);
    }

    public long addMessage(Message msg) throws Exception {

        ContentValues values = new ContentValues();
        values.put(DBContract.MessagesTable.COLUMN_TEXT, msg.getText());
        values.put(DBContract.MessagesTable.COLUMN_FILE, msg.getFile());
        values.put(DBContract.MessagesTable.COLUMN_TYPE, msg.getType());
        values.put(DBContract.MessagesTable.COLUMN_TIMESTAMP, msg.getTimeStamp());

        SQLiteDatabase db = this.getWritableDatabase();
        long rowId = db.insert(DBContract.MessagesTable.TABLE_NAME, null, values);
        db.close();

        if (!(rowId > 0)) {
            throw new Exception("Error inserting message into DB");
        }else{
            return rowId;
        }
    }
}

And a ContactDAO class to put Contact objects into the db:

public class ContactDAO extends BaseDAO {

    public ContactDAO(Context context) {
        super(context, DBContract.DB_NAME, null, DBContract.DB_VERSION);
    }

    public long addContact(Contact contact) throws Exception {
        ContentValues values = new ContentValues();
        values.put(DBContract.ContactTable._ID, contact.getId());
        values.put(DBContract.ContactTable.COLUMN_CONTACT_NAME, contact.getContactName());
        values.put(DBContract.ContactTable.COLUMN_CONTACT_TYPE, contact.getContactType());

        SQLiteDatabase db = this.getWritableDatabase();
        long rowId = db.insert(DBContract.ContactTable.TABLE_NAME, null, values);
        db.close();

        if (rowId <= 0) {
            throw new Exception("Error inserting contact into DB");
        } else {
            return rowId;
        }
    }
}

My question is: should I refactor this so that all my DAO classes are put together into one class (and make that one class a singleton), or is this setup generally OK? It's really hard to find the right information on how to do this online as there seems to be multiple ways to handle db's in Android.

Community
  • 1
  • 1
Highway62
  • 800
  • 1
  • 10
  • 25

1 Answers1

1

Too much OOP. There is no reason that all your DAO classes should derive from the open helperclass.

Your DAO classes correspond more or less to tables, so there is a N:1 relationship between DAO classes and the database object. This means that the DAO classes should use a common instance of the open helper class (or that everything should be put into a single class, but you probably don't want that).

CL.
  • 173,858
  • 17
  • 217
  • 259