我有一個sqLite數據庫在我的應用程序中存儲高分。我最近添加了一個新的列(級別),需要升級數據庫,否則應用程序崩潰。如果我手動刪除.db文件工作正常。 我試圖增加數據庫版本,但沒有奏效。 那麼該怎麼做呢?如何升級sqlite數據庫?
這裏是我的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);
}
}
什麼需要添加/更改?
它應該工作。你叫了getWriteableDatabase嗎?看到http://stackoverflow.com/questions/8627075/updating-sqlite-database-versions – svenkapudija 2013-05-12 10:08:34
它會工作,如果你改變'版本號.. .. – Pragnani 2013-05-12 10:12:41
我有一個open()方法在另一個類:public void open( )throws SQLException { \t \t database = dbHelper.getWritableDatabase(); \t \t}我在按鈕的onClick上調用這個方法,所以我打電話給它。 – 2013-05-12 10:15:55