如何处理多个数据库版本更改时,Android应用程序更新 [英] How to deal with multiple database version changes when Android Application update

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

问题描述

感谢在这里阅读我的问题。

thanks for reading my question here.

其实我对如何应对 SQLite的表,当我需要在表中添加一些行多平方米,精简版的版本。

Actually i have some confusion for how to deal with SQLite tables when i need to add some rows in table for multiple sq-lite versions.

我谷歌的东西,发现这样的事情,我们要做的改变表 onUpgrade 方法。
这将是工作顺序更新。

I Google things and found something like this we have to do alter table in onUpgrade method. This would be work sequential update.

Suppose i have 3 devices they have below versions of database
1) 1st device = database version 1
2) 2nd device = database version 2
3) 3rd device = application not installed.

database version 1 = 2 columns
database version 2 = 2+2 columns
database version 3 = 4+2 columns.

所以在这里,如果我需要给更新到Android应用程序的用户,然后我有什么做的?
我有以下的情况。

So here if i need to give update to my android application users then what i have to do ? I have following scenarios.

1) Updated application would be install in 3rd device where still application is not installed.
2) Application should be update from version 1 to version 3 (Device 1st).
3) Application should be update from version 2 to version 3 (Device 2nd).

在这里我可以得到关于如何处理场景3号的想法,但我不知道我怎么能处理方案第一和第二。

Here i can got idea about how to handle scenario number 3 but i do not know how can i handle scenarios 1st and 2nd.

如何能写code所以这将是对所有方案的工作。

How can i write code so it would be work for all scenarios.

onUpgrade的方法是创建数据库时,只调用数据库版本的变化和的onCreate电话。

onUpgrade method would be call only database version changes and onCreate call when database is created.

感谢任何帮助和建议将AP preciated。

Thanks Any help and Suggestion would be appreciated.

推荐答案

从你所提到的东西(例如onUpgrade法)我假设你正在使用SQLiteOpenHelper。

From what you have mentioned (e.g. the onUpgrade method) I assume you are using a SQLiteOpenHelper.

首先更新您的数据库版本为3。

First update your database version to 3.

在的onCreate(SQLiteDatabase DB),更新code,这样它创建的所有表按照最新版本3的模式(如表中有COL1,COL2和COL3)。一个新的安装将触发此code创建一个全新的V3表。

In onCreate(SQLiteDatabase db), update the code so that it creates all the tables as per the latest version 3 schema (e.g the table has col1, col2 and col3). A new install will trigger this code creating a brand new v3 table.

在onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释NEWVERSION),指的是新老版本的参数和更新已经存在相应的表格,例如。

In onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion), refer to the old and new version parameters and update the tables that already exist accordingly, e.g.

if (oldVersion == 1 && newVersion = 2)
{
    //Going from v1 to v2
    db.execSQL("ALTER TABLE mytable ADD COLUMN col2 INTEGER");
}

if (oldVersion == 1 && newVersion = 3)
{
    //Going from v1 to v3
    db.execSQL("ALTER TABLE mytable ADD COLUMN col2 INTEGER");
    db.execSQL("ALTER TABLE mytable ADD COLUMN col3 INTEGER");
}

if (oldVersion == 2 && newVersion = 3)
{
    //Going from v2 to v3
    db.execSQL("ALTER TABLE mytable ADD COLUMN col3 INTEGER");
}

在数据库版本不匹配用户当前有此code被触发。你知道他们有什么版本,所以可以相应修订现有的表,以使它们达到相同的定义中的onCreate找到。当然,你可以做任何你喜欢的内onUpgrade如创建新表并插入/更新/删除数据。

This code is triggered when the database version doesn't match what the user currently has. You know what version they do have so can amend the existing tables accordingly to bring them up to the same definition as found in onCreate. You can of course do anything you like within onUpgrade such as creating new tables and inserting/updating/deleting data.

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

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