초기 데이터베이스가있는 앱 (세계 시간)을 출시했습니다. 이제 데이터베이스 업그레이드로 앱을 업데이트하고 싶습니다.초기 설치시 데이터베이스 버전이 0입니다.
OnUpgrade()에 업그레이드 코드를 입력하고 newVersion을 확인했습니다. 하지만 내 로컬 테스트에서 호출되지 않았습니다 ...
그래서 내가 데이터베이스 버전을 얻으려면 디버그 문을 넣어 그것은 0입니다.
버전이 지정되지 않은 이유는 무엇입니까?
마헤 - 다음
,
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이 아닌 값을 입력하십시오. –
'onCreate()'와'onUpgrade()'의'SQLiteOpenHelper' 구현에 대한 도움을 얻으려면'SQLiteOpenHelper'의'onCreate()'와'onUpgrade()'구현을 보여줘야합니다. – CommonsWare
@Override 공개 무효에서 onCreate (dB의 SQLiteDatabase) {} \t \t @Override \t 공개 무효 onUpgrade (dB의 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