Android SQLite Upgrade without losing data
public class DataHelper extends SQLiteOpenHelper { private static final String dbName="dbName"; private Context context; private SQLiteDatabase db; private final static int version = 1; public static final String SurveyTbl = "CREATE TABLE SurveyTbl (SurveyId TEXT PRIMARY KEY, Idref TEXT, SurveyDate TEXT)"; public DataHelper(Context context) { super(context, dbName, null, version); this.db = getWritableDatabase(); this.context = context; Log.i("", "********************DatabaseHelper(Context context)"); } @Override public void onCreate(SQLiteDatabase db) { try { db.execSQL(SurveyTbl); } catch (Exception e) { Log.i("", "*******************onCreate"); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { try { db.execSQL("ALTER TABLE HandpumpSurveyTbl ADD COLUMN NalYozna TEXT"); } catch (Exception e) { Log.i("", ""+e); } onCreate(db); }}
I didn't get to see your Quiz table schema, but I assume it has fields like "question", "answer", "favorites", and some kind of a unique primary key to identify each question, which I will just call rowId for now.
// after renaming the old table and adding the new tabledb.execSQL("UPDATE new_quiz SET new_quiz.favorites = old_quiz.favorites where new_quiz.rowId = old_quiz.rowId");
That will update only the rows of the new quiz table that match the old quiz table, and set the favorites value from the old quiz table.
I assume you have some kind of a unique identifier to identify each question, so instead of the rowId above, you'll use that (question number or something).