使用内部final String
的方法是一种很好的方法,而不是使用interface
来声明数据库类中的表名和列名。它看起来像这样:
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.
答案 0 :(得分:1)
许多人认为Constant Interface是反模式。它确实有效,但我发现做这样的事情同样成功:
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";
}
对我来说,分别封装每个实体的信息似乎更清晰,更合适。