问题
I am developing the second version of my application. In which I am facing a problem.
In my application the database is in the assets folder. Now I want to update my database from the assets folder in the second version.
I tried the code to upgrade as :
// Data Base Version.
private static final int DATABASE_VERSION = 2;
I changed the version from 1 to 2.
//Constructor
public DataBaseHelperClass(Context myContext) {
super(myContext, DATABASE_NAME, null ,DATABASE_VERSION);
this.context = myContext;
//The Android's default system path of your application database.
DB_PATH = "/data/data/com.example.myapp/databases/";
}
// Create data base
public void createDataBase() throws IOException{
//check if the database exists
boolean databaseExist = checkDataBase();
if(databaseExist){
this.getWritableDatabase();
}else{
this.getReadableDatabase();
try{
copyDataBase();
} catch (IOException e){
throw new Error("Error copying database");
}
}// end if else dbExist
} // end createDataBase().
// Check database
public boolean checkDataBase(){
File databaseFile = new File(DB_PATH + DATABASE_NAME);
return databaseFile.exists();
}
// Copy database from assets
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
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();
}
// Open database
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
// Close database
@Override
public synchronized void close() {
if(sqliteDataBase != null)
sqliteDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// No need to write the create table query.
// As we are using Pre built data base.
// Which is ReadOnly.
}
// On Update database.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion){
context.deleteDatabase(DATABASE_NAME);
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
} else {
}
}
But the issue is that, the database is not updated after executing the apk in the device. Now what should I do.
My applications first version is on the market. Don't know how to update database in the second version.
Please guide me with your valuable suggestions, I am in middle of my application and I am not able to move.
EDIT : onUpgrade not calling. As i tried to print log in 1st line in onUpgrade but it is not printed. So the issue is that, onUpgrade is not calling.
回答1:
In order to upgrade your database you need to do properly by manipulating the schema I found Both the answers for my questions were useful...
Upgrading database version automatically
回答2:
This is mostly answered in this link: Why is onUpgrade() not being invoked on Android sqlite database?
onUpgrade()
will get called in getWriteableDatabase()
or getReadableDatabase()
, so if you never open the DB for writing or reading specifically, you'd have to manually call through to onUpgrade()
or open your database.
回答3:
To test your onUpgdate you should:
- install old version of your app. launch it and let it create a db.
- install new version of your app. This should be done with a special adb command:
adb install -r
.-r
key simulates app update process. Without-r
keyadb install
just deletes old version and intalls new one, so onUpgrade doesn't called.
But you better write a test for this purpose. Google it.
回答4:
I got my answer. The changes I have done are in constructor only as :
public DataBaseHelperClass(Context context) {
super(context, DATABASE_NAME, null ,DATABASE_VERSION);
this.myContext = context;
//The Android's default system path of your application database.
DB_PATH = "/data/data/com.example.myapp/databases/";
}
and it works for me.
回答5:
If we use exist database by copy method, we have to set the current DB version to SQLiteDatabase instance. And each time you want upgrade app call getWritableDatabase(), the onUpgrade() function will be called.
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
Logger.debug(TAG, "checkDataBase dbExist: " + dbExist);
if (dbExist) {
this.getWritableDatabase();
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getWritableDatabase();
try {
copyDataBase();
Logger.debug(TAG, "copyDataBase SUCCESS");
} catch (IOException e) {
throw new Error("Error copying database");
}
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = mContext.getDatabasePath(DB_NAME).getPath();
mDataBase = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
mDataBase.setVersion(DB_VERSION);
return mDataBase;
}
来源:https://stackoverflow.com/questions/20192642/android-onupgrade-not-calling-at-database-upgrade