如何启动调用 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)?
How to initiate the call onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)?
我需要将我的 android sqlite 数据库从版本 1 升级到版本 2。但我不明白调用 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
是如何发起的?如何以及何时调用此方法?我不明白我在哪里传递版本号 2?
[编辑] 我想我需要澄清这个问题。这是我的 class:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context){
super(context,
Constants.DB_NAME,
null,
1);
}
public MySQLiteOpenHelper(Context context, int version){
super(context, Constants.DB_NAME, null, version);
}
public MySQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, Constants.DB_NAME, factory, version);
// TODO Auto-generated constructor stub
}
public MySQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version,
DatabaseErrorHandler errorHandler) {
super(context, Constants.DB_NAME, factory, version, errorHandler);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// do creation stuff
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do upgrade stuff
}
}
那么,android如何触发onUpgrade?
只需增加 DATABASE_VERSION
常量,然后实现 onUpgrade
方法来对应这些更改(例如,创建在版本 2 中新添加的 table)。有一点要明白,onUpgrade不是用来升级数据库版本的,而是在版本升级后更新数据库,赶上新版本。
查看我的 source 了解更多信息。
SQLiteOpenHelper
构造函数采用名称和版本号,助手将根据需要调用 onCreate
或 onUpgrade
回调。您需要自己维护数据库版本号,在需要更改架构时修改版本号。
当您需要将数据库从版本 1 更新到版本 2 时,只需更改
super(context, Constants.DB_NAME, null, 1);
到
super(context, Constants.DB_NAME, null, 2);
然后在您的 onUpgrade
方法中删除表、添加表或执行从 1 升级到 2 所需的任何其他操作。onUpgrade
只会在 SQLiteOpenHelper 第一次实例化之后被调用版本号更改。
我需要将我的 android sqlite 数据库从版本 1 升级到版本 2。但我不明白调用 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
是如何发起的?如何以及何时调用此方法?我不明白我在哪里传递版本号 2?
[编辑] 我想我需要澄清这个问题。这是我的 class:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context){
super(context,
Constants.DB_NAME,
null,
1);
}
public MySQLiteOpenHelper(Context context, int version){
super(context, Constants.DB_NAME, null, version);
}
public MySQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, Constants.DB_NAME, factory, version);
// TODO Auto-generated constructor stub
}
public MySQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version,
DatabaseErrorHandler errorHandler) {
super(context, Constants.DB_NAME, factory, version, errorHandler);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// do creation stuff
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do upgrade stuff
}
}
那么,android如何触发onUpgrade?
只需增加 DATABASE_VERSION
常量,然后实现 onUpgrade
方法来对应这些更改(例如,创建在版本 2 中新添加的 table)。有一点要明白,onUpgrade不是用来升级数据库版本的,而是在版本升级后更新数据库,赶上新版本。
查看我的 source 了解更多信息。
SQLiteOpenHelper
构造函数采用名称和版本号,助手将根据需要调用 onCreate
或 onUpgrade
回调。您需要自己维护数据库版本号,在需要更改架构时修改版本号。
当您需要将数据库从版本 1 更新到版本 2 时,只需更改
super(context, Constants.DB_NAME, null, 1);
到
super(context, Constants.DB_NAME, null, 2);
然后在您的 onUpgrade
方法中删除表、添加表或执行从 1 升级到 2 所需的任何其他操作。onUpgrade
只会在 SQLiteOpenHelper 第一次实例化之后被调用版本号更改。