为什么我们需要 onUpgrade();SQLiteOpenHelper 类中的方法 [英] why we need to onUpgrade(); method in SQLiteOpenHelper class

查看:40
本文介绍了为什么我们需要 onUpgrade();SQLiteOpenHelper 类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习本教程.http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

任何人都可以让我清除这段代码.

can any body please make me clear this chunk of code.

 // Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

问题

onUpgrade(); 方法的目的是什么?

什么时候调用?正如文档所说这是在数据库需要升级时调用升级数据库意味着什么?

When it is Called? as docs says this is Called when the database needs to be upgraded what does it means by upgrading the database?

重要

为什么我们在这种方法中删除表并重新创建?

why we drop the table in this method and recreate?

提前致谢.

推荐答案

onUpgrade 基本上用于处理任何新版本应用的新数据库更改(可能是新列添加、表添加).

onUpgrade is basically for handling new db changes(could be new columns addition,table addition) for any new version of your app.

在 onUpgrade 中并不总是需要删除表,这完全取决于您的用例是什么.如果要求不保留旧版本应用程序中的数据,则 drop 应该会有所帮助,但如果它喜欢更改架构,那么它应该只有更改脚本.

Droping the table is not always necessary in onUpgrade it all depends on what your use case is. If the requirment is to not to persists the data from your older version of app then drop should help,but if its like changing schema then it should only have alter scripts.

这篇关于为什么我们需要 onUpgrade();SQLiteOpenHelper 类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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