我已經發布了具有初始數據庫的應用程序(世界時間)。現在我想用數據庫升級來更新應用程序。初始安裝時數據庫版本爲零
我已經在OnUpgrade()中加入了升級代碼並檢查了newVersion。但它並沒有在我的本地測試中被調用...
所以我把調試語句來獲取數據庫版本,它是零。
任何想法爲什麼它沒有被版本化?
以下是將數據庫從我的資產文件夾中複製代碼...
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
以下是我的構造,在OnCreate()& OnUpgrade()
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion == 1 && newVersion == 2){
String sql = "update statement...."; // i have a correct update statement here
db.execSQL(sql);
}
}
-
馬赫什
http://android.maheshdixit.com
當你讀取檢查-1而不是0。 – 2010-06-06 15:25:41
如果您希望獲得關於'onCreate()'和'onUpgrade()'的'SQLiteOpenHelper'實現方面的幫助,您需要顯示'onCreate()'和'onUpgrade()'''SQLiteOpenHelper'的實現。 – CommonsWare 2010-06-06 17:33:33
@Override 公共無效的onCreate(SQLiteDatabase分貝){ \t} \t @Override \t公共無效onUpgrade(SQLiteDatabase分貝,INT oldVersion,INT NEWVERSION){ \t \t如果(oldVersion == 1 && NEWVERSION == 2){ \t \t \t String sql =「some sql statement ...」; \t \t \t db.execSQL(sql); \t \t} \t} 公共DatabaseHelper(上下文上下文){ \t超級(上下文,DB_NAME,NULL,DB_VERSION); this.myContext = context; } – mahesh 2010-06-06 17:40:10