My class derived from SQLiteOpenHelper is getting huge

后端 未结 5 1077
遥遥无期
遥遥无期 2021-02-19 07:55

My class derived from SQLiteOpenHelper is getting bigger and bigger over a time. At the speaking time it is more than 1500 lines of code which is not considered to

5条回答
  •  情深已故
    2021-02-19 08:17

    I follow as given code snippet:

    SQLHelper class:

        public class SQLHelper extends SQLiteOpenHelper {
            public SQLHelper(Context context, String DBName) 
            {
                super(context, DBName, null, DATABASE_VERSION);
            }
    
            public SQLiteDatabase getDBObject(int isWrtitable) 
            {
                return (isWrtitable == 1) ? this.getWritableDatabase() : this.getReadableDatabase();
            }
    
            @Override
            public void onOpen(SQLiteDatabase db) 
            {
                super.onOpen(db);
                onCreate(db);
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) 
            {
    
                db.execSQL(TABLE_1);
                db.execSQL(TABLE_2);
                ...
                db.execSQL(TABLE_N);
            }
        }
    

    MySQLManager: Which does most of the work:

        public class MySQLManager 
        {
            private SQLHelper sqlHelper;
    
            //Singlton class
    
            public void initMySQLManager(Context context, String DBName) 
            {
                _context = context;
                sqlHelper = new DBHandler(context, DBName);
            }
    
    
            public MyObject getMyObjectRecord() {
                MyObject myObj = new MyObject();
                Cursor cursor = null;
    
                try {
                    cursor = dbObject.getWritableDatabase().rawQuery("select *  FROM " + SQLHelper.MYOBJECT_TABLE + ";", null);
                    //fetch things
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (cursor != null && !cursor.isClosed()) {
                        cursor.close();
                    }
                }
                return bookVo;
            }
    
            //Similarly other methods.
        }
    

提交回复
热议问题