Should there be one SQLiteOpenHelper for each table in the database?

后端 未结 3 475
南笙
南笙 2020-12-15 18:24

Is it better to have a single big SQLiteOpenHelper subclass that defines onCreate and onUpgrade methods for every table in the databas

3条回答
  •  星月不相逢
    2020-12-15 19:27

    Just for the sake of a different approach:

    You can always overried on the onOpen(..) method have it called your onCreate(..) . Be sure to use the "CREATE TABLE IF NOT EXISTS..." statement rather than "CREATE TABLE"

        @Override
    public void onOpen(SQLiteDatabase db) {
        onCreate(db);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_FRIENDS_TABLE = "CREATE TABLE IF NOT EXISTS ...";
        db.execSQL(CREATE_FRIENDS_TABLE);
    }
    

    You do that with every class that extends from SQLiteOpenHelper

提交回复
热议问题