I have a error like yours ,here my code:
try {
String sql = "SELECT * FROM "+DB_TABLEDOWNLOAD;
Cursor cursor = db.rawQuery(sql, null);
//空双引号为原表没有的字段
String temp = "";
int existIconPath = cursor.getColumnIndex("iconPath");
int existAudioID = cursor.getColumnIndex("audioID");
if (existIconPath == -1 && existAudioID == -1){
temp = "url, downed,total,path,name, audioTitle, audioFileID,\"\",\"\"";
}else if (existIconPath == -1 && existAudioID != -1){//iconPath不存在
temp = "url, downed,total,path,name, audioTitle, audioFileID,\"\",audioID";
}else if (existIconPath != -1 && existAudioID == -1){//audioID不存在
temp = "url, downed,total,path,name, audioTitle, audioFileID,iconPath,\"\"";
}else {
return;
}
db.beginTransaction();
String tempTableName = "_temp_"+DB_TABLEDOWNLOAD;
String sqlCreateTemp = " ALTER TABLE "+DB_TABLEDOWNLOAD+" RENAME TO "+tempTableName+";";
db.execSQL(sqlCreateTemp);
final String TB_TESTPAPERINFO_CREATE = "Create TABLE IF NOT EXISTS "
+ DB_TABLEDOWNLOAD
+ "(url TEXT, downed TEXT,total TEXT,path TEXT,name TEXT, audioTitle TEXT, audioFileID TEXT,iconPath TEXT, audioID TEXT);";
db.execSQL(TB_TESTPAPERINFO_CREATE);
String sqlBackupData = "INSERT INTO "+DB_TABLEDOWNLOAD+" SELECT "+temp+" FROM "+tempTableName+";";
db.execSQL(sqlBackupData);
String sqlDrop = "DROP TABLE IF EXISTS '"+tempTableName+"';";
db.execSQL(sqlDrop);
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
}finally{
db.endTransaction();
}
I use return before db.beginTransaction(),my code return before beginTransaction,but I endTransaction in finally . if you do not beginTransaction and endTransaction ,the exception will appear.
so check your code about the db.beginTransaction and endTransaction.