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?

  • And what is the essential difference between the description of costing and writing xml? For example, when I need to work with SQLite, I simply create classes for each table and then in SQLiteOpenHelper I call the methods from these classes onCreate and onUpgrade - Kirill Stoianov
  • Is it possible to sample code with this approach? - Sergey Vodakov
  • Although, probably braking. This is the approach I have given in the question. Just for each table will be your DBHelper. - Sergey Vodakov
  • No, DBHelper, but in it methods from classes of tables will be caused. But essentially nothing changes, except that you have each table in a separate class and DBHelper does not have a heap of constants and other code - Kirill Stoianov
  • one
    If the given XML is not received from somewhere earlier or, for example, is not generated automatically, then this approach can of course be used, but it does not give any advantages, on the contrary, it increases and complicates the code. I just do not see the point in this, since this approach does not give any gain compared to the “classic” one. - pavlofff

1 answer 1

This code does not pretend to the correct solution, just an example!

OptionsTable.java

 public class OptionsTable { // Database table public static final String TABLE_OPTIONS = "table_options"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_DB_VERSION = "db_version"; // Database creation SQL statement private static final String DATABASE_CREATE = "create table " + TABLE_OPTIONS + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_DB_VERSION + " text " + ");"; public static void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { Log.w(PacketTable.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); database.execSQL("DROP TABLE IF EXISTS " + TABLE_OPTIONS); onCreate(database); } } 

DBHelper.java

 public class DBHelper extends SQLiteOpenHelper { private static final String TAG = DBHelper.class.getSimpleName(); public static final String DATABASE_NAME = "application.db"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { OptionsTable.onCreate(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { OptionsTable.onUpgrade(db, oldVersion, newVersion); } }