我的应用程序中有一个存储高分的 sqLite 数据库。我最近添加了一个新列(级别),需要升级数据库,否则应用程序崩溃。如果我手动删除 .db 文件可以正常工作。我试图增加数据库版本,但没有奏效。那么该怎么做呢?
这是我的 sqlite 助手类:
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);
}
}
需要添加/更改什么?