我想添加三个新表到我现有的sqlite数据库,我遇到了成功升级后不更新数据库版本的问题。下面是运行DatabaseHelper:成功升级后Android升级数据库不更新数据库版本
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, "Current db version is " + db.getVersion());
db.execSQL(ORIGINAL_DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
db.execSQL(NEW_TABLE_3_DATABASE_CREATE);
db.setVersion(newVersion);
Log.w(TAG, "Version after onUpgrade is " + db.getVersion());
}
}
下面是我的open()函数: 3210
下面是我的close()函数:
public void close() {
mDb.close();
mDbHelper.close();
}
第一所以会发生什么运行更高DATABASE_VERSION是第一个onUpgrade日志读出: 旧版本1新版本2 db.getVersion是1
第二个日志是:onUpgrade后 版本是2
但是当数据库被再次访问onUpgrade是跑不更新数据库的版本号,并通过onUpgrade与第一日志中运行后onUpgrade阅读: 旧版本1新版本2 db.getVersion是1
然后,应用程序崩溃,因为它尝试创建一个已经存在的表。
我试过在onUpgrade中也没有手动设置数据库版本。这也没有用。我也尝试通过运行更新版本号...
db.execSQL("PRAGMA user_version = " + newVersion);
...在onUpgrade结尾处。
编辑:
每Aswin Kumar的建议,我已经改变了我onUpgrade到备份我现有的表并删除所有表,然后重新创建它们。这并没有解决我的版本问题。下面是我的onUpgrade和的onCreate:
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, "Current db version is " + db.getVersion());
db.execSQL(ORIGINAL_DATABASE_CREATE);
db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
db.execSQL(NEW_TABLE_3_DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
mOldDbContents = backupTables(db);
db.execSQL("DROP TABLE IF EXISTS " + ORIGINAL_DATABASE_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_1_DATABASE_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_2_DATABASE_CREATE);
db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_3_DATABASE_CREATE);
onCreate(db);
}
下面是我用它来创建表的sqlite的语句:
private static final String ORIGINAL_DATABASE_CREATE = "create table " + VEHICLE_EXPENSE_TABLE_NAME +
" (" + KEY_ROW_ID + " integer primary key autoincrement, " +
KEY_UNIX_DATE + " integer, " +
KEY_DATE + " not null default current_date, " +
KEY_TIME + " not null default current_time, " +
KEY_DESCRIPTION + " text, " +
KEY_START_MILE + " integer, " +
KEY_END_MILE + " integer, " +
KEY_MILES + " text, " +
KEY_AMOUNT + " text, "
+ KEY_PARTY_ID + " integer)";
private static final String NEW_TABLE_1_DATABASE_CREATE = "CREATE TABLE " + PURCHASE_HISTORY_TABLE_NAME +
"(" + HISTORY_ORDER_ID_COL + " TEXT PRIMARY KEY, " +
HISTORY_STATE_COL + " INTEGER, " +
HISTORY_PRODUCT_ID_COL + " TEXT, " +
HISTORY_DEVELOPER_PAYLOAD_COL + " TEXT, " +
HISTORY_PURCHASE_TIME_COL + " INTEGER)";
private static final String NEW_TABLE_2_DATABASE_CREATE = "CREATE TABLE " + PURCHASED_ITEMS_TABLE_NAME +
"(" + PURCHASED_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " +
PURCHASED_QUANTITY_COL + " INTEGER)";
private static final String NEW_TABLE_3_DATABASE_CREATE = "CREATE TABLE " + TRIAL_LIMIT_TABLE_NAME +
"(" + TRIAL_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " +
TRIAL_PRODUCT_NAME + " TEXT, " +
TRIAL_START_DATE + " INTEGER)";
任何帮助将是巨大的赞赏。
谢谢 凯文
2012-07-27
KevinM