First set up a base class for table:
Public abstract class Dbbasetable {private static final String TAG = "dbbasetable";/** * @return the DB table name
*/abstract String getName (); /** * Creates the DB table according to the DB scheme * * @param db * /abstract void OnCreate (Sqlitedataba SE db); void Onupgrade (sqlitedatabase db, int oldversion, int newversion, String tempname) { } void Joincolumns ( Sqlitedatabase db, String tempname, String tableName) { dbutils.joincolumns (db, Tempname, tableName);} }
We build the table this way:
Public final class DatabaseClass {private DatabaseClass () {}; public static final int db_version = 2; Static final String db_file = "my.db"; <strong>//place public fields </strong> common interface Rcmcolumns {Publ IC static final String account_id = "account_id"; INTEGER (Long)} public static final class Mytest1table extends Dbbasetable implements Basecolumns, Rcmcolumns {Private mytest1table () {}private static final mytest1table sinstance = new mytest1table (); static mytest1table Getinstanc E () {return sinstance;} Private static final String table_name = "MYTEST1";/* Columns */public static final String TEST1 = "Test1";p ublic static F Inal string TEST2 = "Test2";p ublic static final string test3= "Test3";p rivate static final String create_table_stmt = "CRE ATE TABLE IF not EXISTS "+ table_name +" ("+ _id +" integer PRIMARY KEY autoincrement, "+ account_id +" integer, "+ TEST1 + "text,"//+ TEST2 + "text," + TEST3 + "text" + ");"; @OverrideString GetName () {return table_name;} @Overridevoid onCreate (Sqlitedatabase db) {db.execsql (create_table_stmt);}} public static final class Mytest2table extends Dbbasetable implements Basecolumns, Rcmcolumns {private mytest2table () {}PR Ivate static final mytest2table sinstance = new mytest2table (); static mytest2table getinstance () {return sinstance;} Private static final String table_name = "MYTEST2";/* Columns */public static final String TEST4 = "Test4";p ublic static F Inal string test5= "Test5";p ublic static final string test6= "Test6";p rivate static final String create_table_stmt = "CREA TE TABLE IF not EXISTS "+ table_name +" ("+ _id +" integer PRIMARY KEY autoincrement, "+ account_id +" integer, "+ T EST4 + "text," + TEST5 + "text," + TEST6 + "text" + ");"; @OverrideString GetName () {return table_name;} @Overridevoid onCreate (Sqlitedatabase db) {db.execsql (create_table_stmt);}} Static linkedhashmap<string, dbbasetable> srcmdbtables = new linkedhashmap&Lt String, dbbasetable> (); static {Srcmdbtables.put (Mytest1table.getinstance (). GetName (), mytest1table.getinstance ()); Srcmdbtables.put (Mytest2table.getinstance (). GetName (), mytest2table.getinstance ()); }}
Android Some SQL statements are executed to rely on,Sqliteopenhelper This class,
Let's create a subclass of this class:
Final class DBHelper extends Sqliteopenhelper {private static final String TAG = "[Rc]rcmdbhelper"; private static final String Temp_suffix = "_temp_"; Private sqlitedatabase db_r = null; Readable database private sqlitedatabase db_w = null; Writable database private static DBHelper dbhelper; Private DBHelper (Context context) {<strong> Super (context, databaseclass.db_file, NULL, databaseclass.db_vers ION);</strong>} public static synchronized DBHelper getinstance (context context) {if (DBHelper = = Nu ll) {dbhelper = new DBHelper (context); } return dbhelper; } @Override public void OnCreate (Sqlitedatabase db) {<strong> collection<dbbasetable> tab Les = DataBaseClass.sRCMDbTables.values (); iterator<dbbasetable> Iterator = Tables.iterator (); System.out.println ("====dbhelp oncreate"); try {db.begintransaction (); while (IteraTor.hasnext ()) {Iterator.next (). onCreate (DB); } db.settransactionsuccessful (); } catch (Throwable e) {//todo Implement proper error handling throw new Runtimeexceptio N ("DB creation failed:" + e.getmessage ()); } finally {db.endtransaction (); }</strong>} @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {} private string Gettemptablename (String tableName, collection<string> oldtablenames, set<string> newtabl Enames) {String temp_name_base = tableName + temp_suffix; if (!oldtablenames.contains (temp_name_base) &&!newtablenames.contains (temp_name_base)) {return temp_n Ame_base; } Random random = new random (); String Temp_name; for (;;) {temp_name = temp_name_base + random.nextint (); if (!oldtablenames.contains (temp_name) &&!newtablenames.contains (temp_name)) {return temp_name; }}} @Override public synchronized Sqlitedatabase getreadabledatabase () {if (Db_r = = NULL | | !db_r.isopen ()) {try {<strong>db_r = super.getreadabledatabase ();</strong> } catch (Sqliteexception e) {//todo Implement proper error handling db_r = NULL; Throw e; }} return db_r; } @Override Public synchronized Sqlitedatabase getwritabledatabase () {if (Db_w = = NULL | |!db_w.isopen () || Db_w.isreadonly ()) {try {<strong> db_w = super.getwritabledatabase ();</strong> } catch (Sqliteexception e) {//todo Implement proper error handling db_w = NULL; Throw e; }} return db_w; }}