我正在尝试向现有的sqlite数据库添加三个新表,并且我遇到了在升级成功后db版本未更新的问题。下面是运行的DatabaseHelper:
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()函数:
public VehicleExpDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
mDb = mDbHelper.getWritableDatabase();
return this;
}
下面是我的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版本。这也不起作用。我也试过运行...
来更新版本号db.execSQL("PRAGMA user_version = " + newVersion);
...在onUpgrade结束时。
编辑:
Per 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)";
任何帮助都会非常感激。
谢谢你, 凯文
答案 0 :(得分:3)
在onUpgrade()
中,您必须drop
现有表(在将数据迁移到某个临时数据结构之后),然后创建新表(使用数据结构中的旧数据)。如果不是,旧表仍然存在。
如果这不能解决问题,请在db.execSQL()
答案 1 :(得分:1)
修正了我自己的问题。问题是我创建了一个单独的DB帮助程序类,它正在访问尝试升级的同一个DB。在这个单独的DB帮助程序类中,版本号设置为1.这个应用程序是我的第一个Java和Android程序,我设置了可怕的数据库类。我已将所有数据库类合并到我正在升级的数据库中,现在我的数据库版本正在按照它应该更新。如果有人有兴趣,下面是我的最后课程。
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, "Current db version is " + db.getVersion());
db.execSQL(VEHICLE_EXPENSE_DATABASE_CREATE);
db.execSQL(PURCHASE_HISTORY_DATABASE_CREATE);
db.execSQL(PURCHASE_ITEMS_DATABASE_CREATE);
db.execSQL(TRIAL_TIME_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());
if (oldVersion < newVersion) {
if (oldVersion == 1) {
db = upgradeTo2(db);
}
}
}
private SQLiteDatabase upgradeTo2(SQLiteDatabase db) {
db.beginTransaction();
try {
db.execSQL(PURCHASE_HISTORY_DATABASE_CREATE);
db.execSQL(PURCHASE_ITEMS_DATABASE_CREATE);
db.execSQL(TRIAL_TIME_DATABASE_CREATE);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.w(TAG, "onUpgrade failed");
} finally {
db.endTransaction();
}
return db;
}