Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

android - what is the correct way to upgrade sqllite db?

private static final String TABLE_MAIN_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_MAIN_NAME + " ( a INTEGER, b LONG, c TEXT, d TEXT, e DATETIME, f TEXT)";
private static final String TABLE_MAIN_UPGRADE = "ALTER TABLE " + TABLE_MAIN_NAME + " ADD Column f TEXT";

    @Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_MAIN_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < newVersion) {
        db.execSQL(TABLE_MAIN_UPGRADE);
    }
    onCreate(db);
}

The previous db version was without the "f" field. Is this the correct way to upgrade the DB? Do I need onCreate(db) in onUpgrade?

I did this several times and each time I got another exception: 1) table already exists so I added "IF NOT EXISTS" 2) could not read row 0, col 5 error here I lost it....

IS this the correct way to upgrade the db?

I want to keep my data

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need not call onCreate in onUpgrade. Doing db.execSQL(TABLE_MAIN_UPGRADE) in enough for update. Now you should clear data of your application. To do this re-install the app or execute in android shell

pm clear your.app.package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...