发布Android应用后,如何向SQLite数据库添加新列? [英] How can I add new columns to a SQLite database after the Android app is released?

查看:248
本文介绍了发布Android应用后,如何向SQLite数据库添加新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向SQLite数据库添加新列,但是我已经在Play商店中发布了我的应用,因此,如果我对其进行编辑,则用户需要卸载并重新安装该应用, 但我不想要那样请帮忙,我是Android的新手.

I want to add new columns to a SQLite database, but I already released my app on the Play Store so, if I edit it, users need to uninstall and reinstall the app, but I don't want that. Please, help, I am new to Android.

推荐答案

(1)递增(或简单地更改)数据库版本
(2)这将导致onUpgrade()方法调用
(3)在onUpgrade()方法中执行查询(用于添加新列).

(1) Increment (or simply change) your database version
(2) It would lead to onUpgrade() method call
(3) Execute your query(for adding a new column) in the onUpgrade() method.

在此博客中.

有时用户可能会从1.1版本更新1.5版本,换句话说,他们可能会跳过1.1到1.5之间的其他版本.您可能将数据库的时间更改为1.1到1.5.因此,为了使用户受益于所有数据库更改,您需要采用如下所示的onUpgrade()方法.

Sometimes user may update the version 1.5 from the version 1.1.In other words, They may skip the other versions between the 1.1 to 1.5. You might changed database couple of time between 1.1 to 1.5. So in order to give the user a benefit of all the database changes, you need to take of onUpgrade() method as below.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   if (oldVersion < 2) {
        db.execSQL(DATABASE_ALTER_TEAM_1);
   }
   if (oldVersion < 3) {
        db.execSQL(DATABASE_ALTER_TEAM_2);
   }
 }

希望有帮助.

这篇关于发布Android应用后,如何向SQLite数据库添加新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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