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

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

问题描述

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

解决方案

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

本博客中提到了正确的做法.p>

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

@Override公共无效 onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){如果(旧版本 <2){db.execSQL(DATABASE_ALTER_TEAM_1);}如果(旧版本 <3){db.execSQL(DATABASE_ALTER_TEAM_2);}}

希望对你有帮助.

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) 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.

The Right Way of doing is mentioned here in this blog.

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);
   }
 }

Hope it helps.

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

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