Here is the solution that I eventually completed. This is a kind of mess from the information received in the Commonsware books, and some things on the Internet that I would like to bookmark, I want to pay tribute to:
For each type of data I need to extract from db, I create an adapter class (not subclassed by anything). These adapter classes contain all the methods needed to access db for this piece of information. For example, if I had three tables in my db:
I would have three adapters that look the same as the following (I just put one as a demo, but the idea is the same for everyone):
public class CarsDBAdapter { public static final String ROW_ID = "_id"; public static final String NAME = "name"; public static final String MODEL = "model"; public static final String YEAR = "year"; private static final String DATABASE_TABLE = "cars"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private final Context mCtx; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public CarsDBAdapter(Context ctx) { this.mCtx = ctx; } public CarsDBAdapter open() throws SQLException { this.mDbHelper = new DatabaseHelper(this.mCtx); this.mDb = this.mDbHelper.getWritableDatabase(); return this; } public void close() { this.mDbHelper.close(); } public long createCar(String name, String model, String year){ ContentValues initialValues = new ContentValues(); initialValues.put(NAME, name); initialValues.put(MODEL, model); initialValues.put(YEAR, year); return this.mDb.insert(DATABASE_TABLE, null, initialValues); } public boolean deleteCar(long rowId) { return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
So, if you imagine that I have one of these classes “adapters” for each table.
When the application splash screen starts, I use the presented Android For Beginners technique : creating multiple SQLite tables for Android
So, my main DBAdapter (which is responsible for creating all my tables in one db) looks like this:
public class DBAdapter { public static final String DATABASE_NAME = "stuffIOwn"; //$NON-NLS-1$ public static final int DATABASE_VERSION = 1; private static final String CREATE_TABLE_CARS = "create table cars (_id integer primary key autoincrement, " //$NON-NLS-1$ + CarsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$ + CarsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$ + CarsDBAdapter.YEAR+ " TEXT" + ");"; //$NON-NLS-1$ //$NON-NLS-2$ private static final String CREATE_TABLE_BOATS = "create table boats (_id integer primary key autoincrement, " //$NON-NLS-1$ +BoatsDBAdapter.NAME+" TEXT," //$NON-NLS-1$ +BoatsDBAdapter.MODEL+" TEXT," //$NON-NLS-1$ +BoatsDBAdapter.YEAR+" TEXT"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$ private static final String CREATE_TABLE_CYCLES = "create table cycles (_id integer primary key autoincrement, " //$NON-NLS-1$ +CyclesDBAdapter.NAME+" TEXT," //$NON-NLS-1$ +CyclesDBAdapter.MODEL+" TEXT," //$NON-NLS-1$ +CyclesDBAdapter.YEAR+" TEXT"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$ private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; /** * Constructor * @param ctx */ public DBAdapter(Context ctx) { this.context = ctx; this.DBHelper = new DatabaseHelper(this.context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_CARS); db.execSQL(CREATE_TABLE_BOATS); db.execSQL(CREATE_TABLE_CYCLES); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Adding any table mods to this guy here } } /** * open the db * @return this * @throws SQLException * return type: DBAdapter */ public DBAdapter open() throws SQLException { this.db = this.DBHelper.getWritableDatabase(); return this; } /** * close the db * return type: void */ public void close() { this.DBHelper.close(); } }
The DBAdapter class is called only at the first start of the application and its sole responsibility is to create / update tables. All other data access is through a separate “adapter”. I found that this works fine and does not create problems with the versions I mentioned earlier.
Hope this helps.