Instead of using final String
s for declaring table and column names inside a database class, does approach of using inner interface
s is a good approach. It looks like this:
public class MyDb extends SQLiteOpenHelper {
//... fields, constructor,etc.
interface Tables {
String T1 = "t1";
String T2 = "t2";
//etc.
}
interface T1Columns {
String T1_ID = "t1_id";
String Column1 = "c1";
String Column2 = "c2";
//etc.
}
//other data
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + Tables.T1 + " ("
+ Columns.T1_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
//etc.
Many people consider the Constant Interface to be an anti-pattern. It certainly works, but I find just as much success doing something like this:
public final class Entity {
public static final String TABLE = "table_name";
public static final String COLUMN_1 = "column1_name";
public static final String COLUMN_2 = "column2_name";
}
To me it seems cleaner and more proper to encapsulate the information of each entity separately.