【发布时间】:2011-09-23 21:08:17
【问题描述】:
我正在尝试在我的 Android 应用程序中更新我的数据库。当我更新版本号时,会调用 onUpgrade,但版本号不会增加,所以每次访问数据库时,都会调用 onUpgrade。这是我的代码:
private final static int DB_VERSION = 8;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "in onCreate");
try {
copyDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
myContext.deleteDatabase(DB_NAME);
Log.d(TAG, "the version is " + db.getVersion());
db.setVersion(newVersion);
Log.d(TAG, "the version is " + db.getVersion());
onCreate(db);
}
有人知道为什么会这样吗?
【问题讨论】:
-
也许这个链接可能会有所帮助stackoverflow.com/questions/7173896/…
-
我已经看过这个链接了……我还是想不通。即使数据库版本号是静态的,版本号也不会在 onUpgrade() 中升级。调用了 OnUpgrade,但之后版本号没有更新...
-
调用 onUpgrade 时 oldVersion 和 newVersion 的值是多少?