我需要升级我的android数据库,新版本是2,并在我的应用程序中调用SQLiteOpenHelper的onUpgrade()。但似乎数据库没有变化。我的操作删除一列并为Student表添加一个新列。android sqlite db onUpgrade失败的SQL执行,但没有变化
升级前,学生有(_phone,_name,_id,_age)四列;
升级后,例外的学生有(_phone,_name,_id,_address)列(删除_age和添加_address列);
但是我失败了,最终结果是学生结构没有改变。
String createTempTable = "CREATE TEMPORARY TABLE student_temp_1(_phone TEXT,_name TEXT,_id INTEGER PRIMARY KEY AUTOINCREMENT);";
String insertTempTable = "INSERT INTO student_temp_1 SELECT _phone,_name,_id FROM student;";
String dropTable = "DROP TABLE student";
String createTable = "CREATE TABLE student (_address TEXT,_phone TEXT,_name TEXT,_id INTEGER PRIMARY KEY AUTOINCREMENT);";
String insertTable = "INSERT INTO student(_phone,_name,_id) SELECT _phone,_name,_id FROM student_temp_1;";
String dropTempTable = "DROP TABLE student_temp_1;";
db.beginTransaction();
db.execSQL(createTempTable);
db.execSQL(insertTempTable);
db.execSQL(dropTable);
db.endTransaction();
// here I execute the PRAGMA TABLE_INFO(student); it returned empty. so the sql is executed.
db.beginTransaction();
db.execSQL(createTable);
db.execSQL(insertTable);
db.execSQL(dropTempTable);
db.endTransaction();
嗨Jamling Li,你能告诉我们onUpdate()方法吗? – KeLiuyue
使用'beginTransaction(); ...... endTransaction();'将无效。您需要编写**'setTransactionSuccessful()'**来表示在'endTransaction();之前应该提交事务(即更改是OK);' – MikeT