如何在不丢失现有的数据升级Android应用程序的版本己方SQLite数据库 [英] how to upgrade the android app version one Own SQLite Database without losing the existing data

查看:144
本文介绍了如何在不丢失现有的数据升级Android应用程序的版本己方SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新我的Andr​​oid应用程序在市场上下一version.next版本需要更新sqlite的DB不失exsiting数据。

在一个版本我没有创建的表中运行,而不是得到数据库文件,从资产文件夹,复制到我的应用程序的系统数据库的路径。 参考此链接 的http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

我的应用程序的

下一个版本,我有修改exsting表列,并添加额外的记录,并有作为well.I已经更新了数据库,并复制到资源文件夹一些新表。

这将适用于谁买了第一次应用程序的用户,而不是现有的用户,我的问题是我怎么可以更新的版本,而不会丢失现有数据的一个用户数据的基础上

山姆。

解决方案

  @覆盖
    公共无效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);
        }
    }
}    

这篇关于如何在不丢失现有的数据升级Android应用程序的版本己方SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆