如何将新列添加到 Android SQLite 数据库?

问题描述:

我对 Android SQLite 数据库有一个问题.

I have one problem with Android SQLite database.

我有一张表,其中包含一个字段.StudentFname并且该应用程序在 Android 2.3.1 上运行良好,现在如果我添加另一个字段,则我的应用程序无法正常工作.

I have one table which contains one field.StudentFname and that application is working fine with Android 2.3.1 and now if I add another field then my application is not working properly.

有懂数据库的可以帮帮我吗,

Can anyone help me who knows database very well,

你可以在你的 onUpgrade() 方法上使用 ALTER TABLE 函数,像这样:

you can use ALTER TABLE function on your onUpgrade() method, like this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // If you need to add a column
  if (newVersion > oldVersion) {
     db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
  }
}

显然,SQLite 会因列定义而异.

Obviously, the SQLite will differ depending on the column definition.