且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发布Android应用后,如何向SQLite数据库添加新列?

更新时间:2023-11-30 15:17:58

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

希望有帮助.