I was just starting to work on a database application when I realized that I had to implement the MVC , because the application is quite complex and includes several database operations.
In this regard, I decided to create a separate model class for processing database operations. This class will have all the methods that will return data to me after executing the Sqlite command (select for example) OR just execute the SQLite command (for example, Delete). But I want to separate this class from the Database Adapter class, where I open, create and close my database.
Let me put my concept in code:
public class DataModel { /* Private members */ // Method to Select data from Student table public ArrayList<String> FetchStudents (parameter 1) { private ArrayList<String> arrStudent; DatabaseAdapter objDB= new DatabaseAdapter(); objDB.open(); /* Some code */ objDB.close(); return arrStudent } //Method to delete record from Student table public DeleteStudent(parameter 1) { DatabaseAdapter objDB= new DatabaseAdapter(); objDB.open(); //Some code objDB.close(); } /* Rest of methods */ }
// DatabaseAdapterClass
private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "Creating DataBase: " + CREATE_STUDENT_TABLE); db.execSQL(CREATE_STUDENT_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); } }
Question :
What I want to ask is the right approach to implementation? Is this fine if you create a separate class for the database methods? What are the limitations or problems that you think may bother me later? Also, is there a better way to implement the above concept?
thanks
A rock
user790431
source share