This is a fairly broad question and a little depends on your level of experience and use.
But a common practice is to create your own ContentProvider , which will abstract access to the database. This way you can use Uri to execute select / update / delete / insert queries.
For the SQLite database SQLiteOpenHelper , use SQLiteOpenHelper to distract the creation and updating of your SQLite database. This will allow you to update your database without losing all your data.
I can attach the part of the code that I used in one of my old projects that you started. But the implementation of all this may fall outside the scope of one question / answer.
public class MyServiceProvider extends ContentProvider { private SQLiteDatabase db; @Override public boolean onCreate() {
and then you can use cursors to interact with the database.
ContentResolver contentResolver = getContentResolver(); ... Cursor c = contentResolver.query( // The Uri results in content://com.mycompany.myApp.MyAppService/myTableOrIdentifier/someId Uri.withAppendedPath(MyServiceProvider.CONTENT_URI, someId), new String[] { // only get fields we need! "MyDbFieldIneed" }, null, null, null);
This will return a Cursor where you can iterate and get the results. This is also how most things are implemented in Android (i.e., getting the address from the address book works through Uri and Cursor too).
Edit: I realized that links are hard to see with code highlights. Here you will need links of important classes.
Edit 2: Also, if you work with multiple tables, UriMatcher also an important source
Tseng source share