2

I need multiple tables in my application. For this I've created separate subclasses of SQLiteOpenHelper to insert/remove/update data for different tables. My question is how can I make sure all these subclasses uses same instance of SQLiteDatabase across the entire code. Though I've made each subclass a singleton. However, I couldn't work around the issue of using a common SQLiteDatabase instance across the entire code.

PS: I don't want to use a ContentPovider or create a single SQLiteOpenHelper subclass as this would complicate my code.

2 Answers 2

1

As long as you're using the same SQLiteOpenHelper throughout (per database) you will be fine. It automatically makes sure that getWriteableDatabase and getReadableDatabase access only one cached database.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is I've multiple tables. Hence I'm using multiple instances of SQLiteOpenHelper to reduce complexity. Do you have a better approach ?
Yes. If you're using multiple tables but they're all in the same database you should absolutely only use one SQLiteOpenHelper. Otherwise you run the risk of some of the writes failing. The easiest is to use just on helper which takes care of that one database.
Agree. The problem here is that SQLiteOpenHelper toy examples usually derive in dirty code when you need multiples tables and there are foreign keys, etc. The helper needs to know about table and row names for creating and upgrading the DB. That's too much coupling, and usually a nightmare of constants on the top of the class appears. If you want to use the helper in a clean and maintenable manner, create a DAO interface with the methods onCreate(SQLiteDatabase db) and onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion). Then in the helper inject your DAOs and callback to each one
0

I don't really see where you're struggling at using a common SQLiteOpenHelper. You just have to duplicate what you do for one table ! Create a custom class that extends SQLiteOpenHelper and do the duplication.

public class SQLiteCustomBase extends SQLiteOpenHelper {

    private static final String CREATE_BDD_1 = "CREATE TABLE ...";
    private static final String CREATE_BDD_2 = "CREATE TABLE ...";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BDD_1);
        db.execSQL(CREATE_BDD_2);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE " + Vars.TABLE_1 + ";");
        db.execSQL("DROP TABLE " + Vars.TABLE_2 + ";");
        onCreate(db);
    }
}

and then in the class where you perform some database actions :

public class HandlerClass {
    private SQLiteDatabase db;
    private SQLiteCustomBase customBase;

    public HandlerClass(Context context){
        customBase = new SQLiteCustomBase(context, Vars.NAME_DB, null, Vars.VERSION_DB);
    }

    public void open(){
        db = customBase.getWritableDatabase();
    }

    public void openForRead(){
        db = customBase.getReadableDatabase();
    }

    public void close(){
        db.close();
    }

    public SQLiteDatabase getDB(){
        return db;
    }
}

void myMethods()
{
    bdd.insert(Vars.TABLE_1, null, values);
    bdd.insert(Vars.TABLE_2, null, values);
}
etc.

1 Comment

Thanks. I've also made SQLiteCustomBase class a singleton so that all HandlerClass uses same instance of SQLiteDatabase throughout application. This avoids any crashes during simultaneous execution !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.