I have an sqLite database in my app which store high scores. I recently added a new column(levels) and need to upgrade the database, otherwise the app crashes. If I delete manually the .db file works fine. I tried to increase the database version but didn't work. So how to do it?
Here is my sqlite helper class:
public class DataBaseHelper extends SQLiteOpenHelper{
public static final String TABLE_HISCORES = "hiScores";
static final String RANK = "_id";
static final String NAME = "name";
static final String SCORE = "score";
static final String LEVEL = "level";
private static final String DATABASE_NAME = "scoresdb.db";
private static final int DATABASE_VERSION = 1;
SQLiteDatabase db;
private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_HISCORES + " ("
+ RANK + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME + " TEXT, "
+ LEVEL + " TEXT, "
+ SCORE + " INTEGER );";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(DataBaseHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISCORES);
onCreate(db);
}
}
What is needed to add/change?