You need to understand SQLiteOpenHelper for this. This is workflow issue, so it would be better you should go through some tutorial. This is a nice tutorial where you can learn the concept.
In very short i am listing few points that may be useful for you:
In your application you create a subclass of the SQLiteOpenHelper class.SQLiteOpenHelper is a helper class to manage database creation and version management. In subclass override, onCreate() and onUpgrade().
public class MySQLiteHelper extends SQLiteOpenHelper { public void onCreate(SQLiteDatabase database) { // create database command. } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // upgrade database command here. }
}
Create a DAO class that will manage the interaction with the database. Your CRUD methods will go here. See DAO pattern for details. This class will centralize the access to database, hence your activity, fragment will interact with this class to perform operation. Direct access to database won't be allowed.
public class ModelDataSource { // needed to perform operation on database private SQLiteDatabase database; //needed to retrieve database object private MySQLiteHelper dbHelper; public boolean insertModel(Model model) { // perform insert operation on database } }
In your activity, you can interact with DAO to perform some action.
public class YourActivity extends Activity { private ModelDataSource datasource; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); datasource = new ModelDataSource(this); datasource.open(); boolean insertion_status = datasource.insert(modelobject); } }