Android SQLite数据库和应用程序更新 [英] Android SQLite database and app update

查看:67
本文介绍了Android SQLite数据库和应用程序更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个Android应用程序,并发布了1.0版.该应用程序包含具有5个表的SQLite本地数据库.

I have developed an android app and released version 1.0. The app contains SQLite local database with 5 tables.

现在,我们计划发布2.0版,同时也更新了1.0版用户.在2.0版中,我们在前面的5个表中增加了两个表,所以现在增加了7个表.

Now we planned to release version 2.0 also update the version 1.0 users. In version 2.0 we have included extra two tables with previous 5 table, so 7 tables now.

现在我的问题是,1.0版的用户在本地数据库中都有一些数据,如果他更新到2.0版,以前的数据会丢失吗?如果是这样.那么什么是替代方法?

Now my question is, The version 1.0 users all have some data in the local database, If he update to the version 2.0 the previous data will get lost? If it so. then what is the alternate method?

推荐答案

您应该将所有更改都放在可以使用以下代码的onUpgrade方法中:

You should put all changes in your onUpgrade method you can use this code:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "ALTER TABLE " + TABLE_SECRET + " ADD COLUMN " +
     "name_of_column_to_be_added" + " INTEGER";
    db.execSQL(sql);        
}        

这将在当前数据库中添加一列.您的数据库不会丢失数据.提醒:当执行getWriteableDatabase或getReadableDatabase并且数据库版本与旧版本不同时,将调用onUpgrade.

this adds a column in your current database. Your database will not lose data. Reminder: onUpgrade will be called when getWriteableDatabase or getReadableDatabase is executed AND the version of your database is different from your older version.

这篇关于Android SQLite数据库和应用程序更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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