About ORM heard, but for now I want to do without them. I met this way of working when in a class inherited from SQLiteOpenHelper, the structure of the database is described in static strings. Like that:
public class DBHelper extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "reminder_database"; public static final String TASKS_TABLE = "tasks_table"; public static final String TASK_TITLE_COLUMN = "task_title"; public static final String TASK_DATE_COLUMN = "task_date"; public static final String TASK_PRIORITY_COLUMN = "task_priority"; public static final String TASK_STATUS_COLUMN = "task_status"; public static final String TASK_TIME_STAMP_COLUMN = "task_time_stamp"; private static final String TASKS_TABLE_CREATE_SCRIPT = "CREATE TABLE " + TASKS_TABLE + " (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_TITLE_COLUMN + " TEXT NOT NULL, " + TASK_DATE_COLUMN + " LONG, " + TASK_PRIORITY_COLUMN + " INTEGER, " + TASK_STATUS_COLUMN + " INTEGER, " + TASK_TIME_STAMP_COLUMN + " LONG);"; public static final String SELECTION_STATUS = DBHelper.TASK_STATUS_COLUMN + " = ?"; private DBQueryManager queryManager; private DBUpdateManager updateManager; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); queryManager = new DBQueryManager(getReadableDatabase()); updateManager = new DBUpdateManager(getWritableDatabase()); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(TASKS_TABLE_CREATE_SCRIPT); } But I do not like this approach. Decided to describe the table structure as XML
<?xml version="1.0" encoding="utf-8"?> <database xmlns:android="http://schemas.android.com/apk/res/android" version = "1"> <table name = "tasks_table"> <column name = "_id" type = "INTEGER" property = "PRIMARY KEY AUTOINCREMENT"/> <column name = "task_title" type = "TEXT" property = "NOT NULL"/> <column name = "task_date" type = "LONG" property = ""/> <column name = "task_priority" type = "INTEGER" property = ""/> <column name = "task_status" type = "INTEGER" property = ""/> <column name = "task_time_stamp" type = "LONG" property = ""/> </table> </database> And then I will write a class that will work with the table using only information from XML. For example, a script to create a table is very easy to generate from this data.
Tell me please, is this approach correct? What rake can I stumble upon in the future? Is this approach applied at all during development? What does this bike look like?