当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Database Ugrade lifecycle

问题描述:

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 / retiving 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:

  1. 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.

  2. 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?

  3. 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)

public class DbWordsAdapter {

private static final String TAG = DbWordsHelper.class.getSimpleName();
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

private static DbWordsHelper dbWordsHelper; 
private Context context;
private SQLiteDatabase mDb;

// database constants
private static final String DB_NAME = "words.db";
private static final int DB_VERSION = 1;
private static final String TABLE_WORDS = "words";

// database columns names
public static final String C_ID = BaseColumns._ID;
public static final String C_WORD = "word";
public static final String C_EXPLANATION = "explanation";
public static final String C_CREATION_DATE = "creation_date";

//Sql Statements
static final String CREATE_TABLE_WORDS = "create table " + TABLE_WORDS
        + "(" + C_ID + " integer primary key autoincrement, " + C_WORD
        + " text not null, " + C_EXPLANATION + " text, "
        + C_CREATION_DATE + " date not null)";
static final String DROP_TABLE_WORDS = "drop table if exists"
        + TABLE_WORDS;      
static final String[] ALL_COLUMNS = { C_ID, C_WORD, C_EXPLANATION,
        C_CREATION_DATE };
static final String ORDER_BY_DATE = C_CREATION_DATE + " desc";
static final String ORDER_BY_ALPH = C_WORD + " asc";
static final String ORDER_BY_RANDOM = "random() limit 1";


/*
 * Inner class that manages database creation and management
 */
private static class DbWordsHelper extends SQLiteOpenHelper {

    private DbWordsHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "SqlCreate Statement: "
                + CREATE_TABLE_WORDS);

        try {
            db.execSQL(CREATE_TABLE_WORDS);
        } catch (SQLException e) {
            Log.e(TAG, "Error while creating database" + TABLE_WORDS, e);
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Here in the future should be method that change the schema of the
        // database. Now we just delete
        try {
            db.execSQL(DROP_TABLE_WORDS);
        } catch (SQLException e) {
            Log.e(TAG, "Error while updating database" + TABLE_WORDS, e);
        }

        onCreate(db);

    }
}

public DbWordsAdapter(Context context) {
    this.context = context;
}

public DbWordsAdapter open () {
    if (dbWordsHelper == null) {
        dbWordsHelper = new DbWordsHelper(context);
    }
    mDb = dbWordsHelper.getWritableDatabase();
    return this;
}

public void close () {
    dbWordsHelper.close();
    dbWordsHelper = null;
}

//
//Following methods implement CRUD operations for this database
//

/*
 * This method creates new Word in the database.
 * 
 * @param word that we create
 * 
 * @param explanation of the word
 * 
 * @return id of the word
 */
public long createWord(String word, String explanation) {
    Log.d(TAG, "createWord method");
    ContentValues newWord = createValue(word, explanation,
            createDateString(new Date()));
    return mDb.insert(TABLE_WORDS, null, newWord);
}

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 in onResume() method and close in onPause()

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.

If you add new columns you can use ALTER TABLE to insert them into a live table. If you 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.

So, basically algorithm for updating in onUpdate should be the following:

  1. At first, according to the oldVersion and newVersion you define what should be updated.
  2. Then if you simply wants to add columns then you call ALTER TABLE ADD COLUMN sql statement. There are some limitations on that. You can find them here.
  3. For deletion and renaming you can use the following advice: If you 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!

分享给朋友:
您可能感兴趣的文章:
随机阅读: