ฉันได้เปิดตัวแอป (เวลาโลก) พร้อมฐานข้อมูลเริ่มต้นแล้ว ตอนนี้ฉันต้องการอัปเดตแอปด้วยการอัปเกรดฐานข้อมูล
ฉันได้ใส่โค้ดอัปเกรดใน OnUpgrade() แล้วตรวจสอบเวอร์ชันใหม่ แต่มันไม่ได้ถูกเรียกในการทดสอบในพื้นที่ของฉัน ...
ดังนั้นฉันจึงใส่คำสั่ง debug เพื่อรับเวอร์ชันฐานข้อมูลและเป็นศูนย์
มีความคิดใดบ้างว่าทำไมมันถึงไม่มีเวอร์ชัน?
ต่อไปนี้เป็นรหัสเพื่อคัดลอกฐานข้อมูลจากโฟลเดอร์ Assets ของฉัน ...
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);
}
}
SQLiteOpenHelper
ของonCreate()
และonUpgrade()
ของคุณ หากคุณคาดว่าจะได้รับความช่วยเหลือเกี่ยวกับการใช้งานonCreate()
และonUpgrade()
ของSQLiteOpenHelper
- person CommonsWare   schedule 06.06.2010