且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不丢失现有的数据升级Android应用程序的版本己方SQLite数据库

更新时间:2023-01-19 14:19:14

  @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,
    INT动态网页)
    {
        Log.w(TAG,从版本升级数据库+ oldVersion
                +来
                +动态网页+,这将销毁所有旧数据);
        如果(oldVersion == 2&安培;&安培; NEWVERSION == 3)
        {
            db.execSQL(ALTER TABLE某某地址鲍比整数默认0);
        }
        其他
        {
            db.execSQL(DROP TABLE IF EXISTS XYZ);
            的onCreate(DB);
        }
    }
}
 

I need to update my android application in the market to next version.next version i need to update the sqlite DB without losing the exsiting data.

In version one i did not create the tables in runtime instead get the database file from the "assets" folder and copies into the system database path of my application. Refer to this link http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

next version of my app i have modify the exsting table columns and add extra records and there are few new tables as well.I have updated the data base and copied to asset folder.

This will work for users who buy the app for first time,but not for existing users,my question is how can i update the version one users data base without losing existing data

Sam.

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        if(oldVersion == 2 && newVersion == 3)
        {
            db.execSQL("ALTER TABLE xyz ADD bobby int default 0");    
        }
        else
        {
            db.execSQL("DROP TABLE IF EXISTS xyz");
            onCreate(db);
        }
    }
}