我对onUpgrade有点困惑。我正在开发我的应用程序,在开发过程中,我确实改变了DATABASE_VERSION
五次,所以它现在是6。
一切顺利,直到我从手机上完全卸载应用程序并再次构建它。从那时起,我遇到了错误,我要求的所有桌子都没有......
我认为现在,当项目中的DATABASE_VERSION
设置为6并且新安装的应用程序在开始时将其设置为6时,onUpgrade()
不会被调用。
问题是如何在onUpgrade方法中为新的instalations提供我设置的所有查询?
我知道无论onUpgrade()
被调用,都会调用getWritableDatabase()
。但是我的应用程序在这条道路上崩溃了:
Cursor spinner_cursor = db.rawQuery("SELECT value1 AS _id, value2 FROM tab2", null);
前面是lane:
SQLiteDatabase db = new DBHelper(this).getWritableDatabase();
我在logCat中收到此错误:
no such table: car (code 1): , while compiling: SELECT value1 AS _id, value2 FROM tab2
我的DBHelper.class看起来像
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "DataBase1";
private static final int DATABASE_VERSION = 6;
private static final String CREATE_QUERY =
"CREATE TABLE tab1 (id, value1, value2, etc)";
private static final String QUERY_2 =
"CREATE TABLE tab2 (id, value1, value2, etc)";
private static final String QUERY_3 =
"CREATE TABLE tab3 (id, value1, value2, etc)";
private static final String QUERY_4 =
"ALTER TABLE tab2 ADD COLUMN value3 VARCHAR(10) NULL";
private static final String QUERY_5 =
"ALTER TABLE tab2 ADD COLUMN value4 VARCHAR(10) NULL";
private static final String QUERY_6 =
"ALTER TABLE tab2 ADD COLUMN value5 VARCHAR(10) NULL";
private static final String QUERY_7 =
"ALTER TABLE tab3 ADD COLUMN value3 VARCHAR(10) NULL";
private static final String QUERY_8 =
"ALTER TABLE tab3 ADD COLUMN value4 VARCHAR(10) NULL";
private static final String QUERY_9 =
"ALTER TABLE tab3 ADD COLUMN value5 VARCHAR(10) NULL";
private static final String QUERY_10 =
"ALTER TABLE tab3 ADD COLUMN value6 VARCHAR(10) NULL";
private static final String QUERY_11 =
"ALTER TABLE tab3 ADD COLUMN value7 VARCHAR(10) NULL";
private static final String QUERY_12 =
"DROP TABLE tab3";
private static final String QUERY_13 =
"CREATE TABLE tab3 (id, value1, value2, etc)";
public DBHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.e("DATABASE OPERATIONS", "Database created / opened");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 3) {
db.execSQL(QUERY_2);
db.execSQL(QUERY_3);
db.execSQL(QUERY_4);
db.execSQL(QUERY_5);
db.execSQL(QUERY_6);
}
if (oldVersion < 4) {
db.execSQL(QUERY_7);
db.execSQL(QUERY_8);
db.execSQL(QUERY_9);
db.execSQL(QUERY_10);
}
if (oldVersion < 5){
db.execSQL(QUERY_11);
}
if (oldVersion < 6){
db.execSQL(QUERY_12);
db.execSQL(QUERY_13);
}
提前致谢!
答案 0 :(得分:2)
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
db.execSQL(QUERY_2);
db.execSQL(QUERY_3);
db.execSQL(QUERY_4);
db.execSQL(QUERY_5);
db.execSQL(QUERY_6);
db.execSQL(QUERY_7);
db.execSQL(QUERY_8);
db.execSQL(QUERY_9);
db.execSQL(QUERY_10);
db.execSQL(QUERY_11);
db.execSQL(QUERY_12);
db.execSQL(QUERY_13);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 3) {
db.execSQL(QUERY_2);
db.execSQL(QUERY_3);
db.execSQL(QUERY_4);
db.execSQL(QUERY_5);
db.execSQL(QUERY_6);
}
if (oldVersion < 4) {
db.execSQL(QUERY_7);
db.execSQL(QUERY_8);
db.execSQL(QUERY_9);
db.execSQL(QUERY_10);
}
if (oldVersion < 5){
db.execSQL(QUERY_11);
}
if (oldVersion < 6){
db.execSQL(QUERY_12);
db.execSQL(QUERY_13);
}