开发者

add new table onUpgrade android

During an upgrade, I want to add a new table to my database, but also not lose the data from the other tables when upgrading the application. Can 开发者_C百科someone tell me, (but if you could show me some example also) of how this is done. Because I've looked through the forums but mainly there are discussions about adding a new column etc. I figured that I have to do it somehow with alter table, but I did not understand everything. If you can tell me the steps of this process I would really appreciate it. Thank u in advance.


If you just want to add a new table and not modify any of your existing tables, then you could just create the new table in your onUpgrade method. This way your existing tables will be untouched.

EDIT: Even better, add the table as usual in onCreate and then in onUpgrade you call onCreate


Try using SQLiteOpenHelper in Android. It has methods for onCreate and onUpgrade.

Sample:

public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

    Maas360Logger.i(loggerName, "upgrading database "+oldVersion+" "+newVersion);
    try {
        database.beginTransaction();
        for (int i = oldVersion + 1; i <= newVersion; i++) {
            // Future schema changes has to go into this loop

Maintain database versions to handle upgrades

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜