Well, this is a question about “best practise” with Android SQL Lite DB’s.
I got my DBHandler which creates a SQLiteDatabase with a inner class called DBHelper like this:
private final DBHelper dbhelper;
SQLiteDatabase db = dbhelper.getReadableDatabase();
// Call the helper
private static class DBHelper extends SQLiteOpenHelper {
DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
// create Table01
String sql = "create table " + "... SQL stuff here";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("drop table if exists " + Table01);
onCreate(db);
}
}
Also the DBHandler offers functions for adding / retrieving data into / from the tables like e.g. this:
public void insertOrIgnore(String table, ContentValues values){
SQLiteDatabase db = this.dbhelper.getWritableDatabase();
try{
db.insertOrThrow(table, null, values);
}
catch (SQLException e){
e.printStackTrace();
Log.e(TAG, e.toString());
}
finally {
db.close();
}
}
public Cursor getTable1(){
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.query(Table1, null, null, null, null, null, TABEL_ID + " ASC");
return cursor;
}
When i need data from the DB i create a DBHandler Object and use its methods.
so, on first application run i call the insertOrIgnore method and add the data i want to use later on.
Whenever i make changes to the DB, i need to uninstall the APP and reinstall it for the changes to take effect. that sucks, so i want to use the DB_VERSION to handle that stuff. After changes to the database (schema OR data) have been made i change the DB_VERSION ID to a higher number.
So my questions are:
- If newDBversion > oldDBversion, SQLiteDatabase dosn’t use onCreate but onUpdate. That works fine, but ONLY the schema is updated
and it results in blank tables. I want to know the best practise so
after a onUpdate the data methods from DBHandler are called again.
Please keep in mind that DBHelper is a inner class of
DBHandler.- For my understanding the SQLiteDatabase onCreate method is ONLY called if the DB is not already present and the onUpdate method is
only called if newDBversion > oldDBversion, is that correct?- Does DBHandler have to be static or a singleton? or is it just fine without, since the SQLiteDatabase class handles multiple read/writes by itself?
I’m open minded for better solutions, but from what i found on the net and in some android books, that kind of adding data to a SQL DB is the way its done. But nobody explains how to update the data (NOT the schema, which kinda is done automaticly whenever dbversion changes)
I can answer the third question because I research a little on this question. I decided that it is good to have the following way of defining these classes (look at my code)
In your application in an appropriate place you simply call
dbWordsAdapter.open();then your db transaction (for instance,dbWordsAdapter.createWord(word, explanation)) and close database. Thus, for activity you can open DB inonResume()method and close inonPause()Considering the second question I think you are rigth.
For the first question I do not know. Maybe it will be good to see how this implemented in other applications.
Update: Answer for the first question
In the documentation and other resources I’ve found the information about the update of the database. You can use sql statement alter table to insert new columns into your database.
So, basically algorithm for updating in onUpdate should be the following:
should be updated.
ADD COLUMN sql statement. There are some limitations on that. You
can find them here.
rename or remove columns you can use ALTER TABLE to rename the old
table, then create the new table and then populate the new table
with the contents of the old table.
Hope this helps!