Android SQLite MultiTable Database Development

后端 未结 1 529
天涯浪人
天涯浪人 2020-12-19 23:16

I would like to create a multi table database with all the RUD (minus the C) in a databasehandler class, but have all the tables created and updated in a helper class.

相关标签:
1条回答
  • 2020-12-20 00:17

    Create a Singleton Database class:

    public class Database extends SQLiteOpenHelper {
    
        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "yourDatabaseName";
    
        //Declare a String for each Table name
        public static final String TABLE_NAME1 = "tableName1";
        public static final String TABLE_NAME2 = "tableName2";
    
        //Declare a SQL string for create each table
        private static final String CREATE_TABLE1 =
                "CREATE TABLE if not exists " + TABLE_NAME1 ..............";
    
        private static final String CREATE_TABLE2 =
                "CREATE TABLE if not exists " + TABLE_NAME2 .................";
    
        private static Database Singleton = null;
        private Context context;
        private static SQLiteDatabase Db;
    
        private Database(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
    
            this.context = context;
        }
    
        public static synchronized Database getInstance(Context c){
            if(Singleton == null) {
                Singleton = new Database(c.getApplicationContext());
                Db = Singleton.getWritableDatabase();
            }
            return Singleton;
        }
    
        public SQLiteDatabase getDatabase() {
    
            return Db;
        }
    
         public synchronized void close() {
    
             if (Singleton != null && Db.isOpen()) Db.close();
         }
    
    
        @Override
        protected void finalize() throws Throwable  {
    
            try{
                close();
            }
            finally{
                super.finalize();
            }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
    
            //Create Tables
            db.execSQL(CREATE_TABLE1);
            db.execSQL(CREATE_TABLE2);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    
        }
    }  
    

    Create base class for Tables. This class has common methods to handle tables

    abstract class DatabaseTable {
    
        private SQLiteDatabase Db;
        private String TableName;
    
        public DatabaseTable(SQLiteDatabase db,String tableName) {
    
            this.TableName = tableName;
            this.Db = db;
    
        }
    
        public Cursor fetchAll() {
    
            Cursor cursor = Db.query(TableName, null, null, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;
        }
    
        public Cursor fetchAllOrderBy(String OrderField) {
    
            Cursor cursor = Db.query(TableName, null, null, null, null, null, OrderField);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;
        }
    
        public Cursor fetchById(long id) {
    
            Cursor cursor = Db.query(TableName, null, "_id = " + id, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;
        }
    
        public boolean deleteById(long id) {
    
            return Db.delete(TableName,"_id = " + id, null) > 0;
        }
    
        public boolean deleteAll() {
    
            return Db.delete(TableName,null, null) > 0;
        }
    
        public int getNumberRows() {
    
            return (int) DatabaseUtils.queryNumEntries(Db, TableName);
        }
    
    }
    

    Now you should define classes for each table you want handle:

    public class TbTable1 extends DatabaseTable{
    
        //Declare string for each field name
        public static final String FIELD_ID = "_id";
        public static final String FIELD_FIELDNAME1 = "fieldName1";
        public static final String FIELD_FIELDNAME2 = "fieldName2";
        ..........
        ..........
    
        private SQLiteDatabase Db;
    
        //Get the table name from Database class
        private static final String TableName = Database.TABLE_NAME1;
    
    
        public TbTable1(SQLiteDatabase db) {
    
            super(db,TableName);
            this.Db = db;
        }
    
        // Below define all the methods you need to access this table
    
        public long insertRecord(String value1, String value2) {
    
            ContentValues initialValues = new ContentValues();
            initialValues.put(FIELD_FIELDNAME2, value1);
            initialValues.put(FIELD_FIELDNAME2, value2);
            return Db.insert(TableName, null, initialValues);
        }
    
        ..............
        ..............
    }  
    

    When you want do anything with a table:

    SQLiteDatabase Db = Database.getInstance(context).getDatabase();  
    TbTable1 table1 = new TbTable1(Db);  
    table1.insertRecord("Value1","Value2");
    Cursor cursor = table1.fetchAll();
    
    // do something with data
    
    cursor.close();
    

    Hope this help!

    0 讨论(0)
提交回复
热议问题