在应用升级行只能升级 [英] Upgrade rows on app upgrade only

查看:185
本文介绍了在应用升级行只能升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想更新数据库行只有一次,只有当我的用户升级自己的应用程序,那么你会怎么做它在OnUpgrade方法在我的数据库处理程序类或基于共享$你会做它作为一个的AsyncTask在应用程序类p $ pferences?

If I want to update DB rows only once only when my users upgrade their app, then would you do it in the OnUpgrade method in my DB handler class or would you do it as an asyncTask in the Application class based on SharedPreferences?

感谢您

推荐答案

在一个应用程序的数据库更新的典型设计模式是这样的东西的code以下,每次更新在需要数据库更改您的应用程序时,你碰到你SQLiteOpenHelper派生类中使用的数据库版本。

The typical design pattern for database updates in an app goes something like the code below and every time you update your application where a database change is required, you bump the database version used in your SQLiteOpenHelper-derived class.

这,当然,你使用presumes SQLiteOpenHelper来管理您的供应商进入您的SQLite数据库的引用:

This, of course, presumes you used SQLiteOpenHelper to manage getting a reference to your SQLite DB in your provider:

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

    if (oldVersion == 1) {
                // DO WORK TO UPGRADE FROM VERSION 1 to 2
                oldVersion += 1;
    }

    if (oldVersion == 2) {
                // DO WORK TO UPGRADE FROM VERSION 2 to 3
                oldVersion += 1;
    }

    if (oldVersion == 3) {
                // DO WORK TO UPGRADE FROM VERSION 3 to 4
                oldVersion += 1;
    }
}

这允许任何用户从任何previous版本升级到最新版本,并确保所有的变化都以正确的顺序进行。因此,这将只能做一次,因为下一个版本,数据库版本变得更高。

This allows any user to upgrade from any previous version to the current version and ensures all the changes are made in the right order. So, it would only be done once, because on the next version, the database version becomes higher.

这篇关于在应用升级行只能升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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