Some people write an enclosing class which has column and table names as well a static class which extends SqlliteOpenHelper class. However it can easily get bloated if you will have more than one table in your database. Is there any code design Android community follows in order to keep this database related code clean? I am still learning Android and I could not really find this on internet. Most people use only one table in tutorials and they don't really care about this aspect.
1 Answer
You can write separate class for DB Helper and separate class for each tables. And write all methods (insert/ update/ delete/ get) into those classes.
I am giving you an example code, which will give you the idea that how to implement the logic.
Helper class
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper sInstance = null;
private static final String DATABASE_NAME = "database_name";
private static final int DATABASE_VERSION = 1;
public static DatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
/**
* Constructor should be private to prevent direct instantiation. make call
* to static factory method "getInstance()" instead.
*/
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Table1.CREATE_TABLE_TABLE1);
db.execSQL(Table2.CREATE_TABLE_TABLE2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// write here for update code
}
}
And tables related separate classes
public class Table1 {
private static final String TABLE = "Table_1";
public static final String CREATE_TABLE_TABLE1 = "CREATE TABLE "
+ TABLE + " (_id INTEGER PRIMARY KEY, name TEXT)";
private Context mContext = null;
public Table1(Context mContext) {
this.mContext = mContext;
}
// And write this Table related stuffs
public long insert(String aStringValue) {
// Here you can get helper as
SQLiteDatabase db = DatabaseHelper.getInstance(this.mContext).getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", aStringValue);
// insert row
return db.insert(TABLE, null, values);
}
public Cursor getAll(String aStringValue) {
//TODO Even instead of getting Cursor you can get List.
// So you have to design some models and fill them into here.
// For example you can read at http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/
// Read above how to create a Model and use them.
String selectQuery = "SELECT * FROM " + TABLE;
SQLiteDatabase db = DatabaseHelper.getInstance(this.mContext).getReadableDatabase();
return db.rawQuery(selectQuery, null);
}
}
// Same as Table_1
public class Table2 {
private static final String TABLE = "Table_2";
public static final String CREATE_TABLE_TABLE1 = "CREATE TABLE "
+ TABLE + " (_id INTEGER PRIMARY KEY, name TEXT)";
// And write this Table related stuffs
// Same as Table 1.
}
3 Comments
DroidDev
this is the best implementation, I was also going to post same answer, but just with a little difference. I create helper class inside tables related class. You can call it DBAdapter. +1
Pankaj Kumar
@VishwasSharma Thank you for such a nice complement. :)
user15
Thank you for your implementation, it's the best from what I've already seen.