what is the correct way to upgrade sqllite db?

前端 未结 4 1359
予麋鹿
予麋鹿 2021-01-16 16:31
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)\";
pr         


        
4条回答
  •  时光说笑
    2021-01-16 16:55

    One way you can do inside onUpgrade method is to drop the table and recreate it. This way, only the data for that particular table will be deleted and not the whole DB data.

    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.w("TAG", "Upgrading database from version " + oldVersion + " to " + newVersion);
        switch (oldVersion) 
        {
            case 1:
                   db.execSQL("DROP TABLE IF EXISTS " + TABLE_MAIN_NAME);
                   db.execSQL(TABLE_MAIN_CREATE);
            default:
                break;
        }
    }
    

提交回复
热议问题