Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite database onUpgrade() does not get called

I am working on an android application where i used existing sqlite database in the asset folder. So i actually copy the database from that folder. User can also save their own data(mark any content as favorite).

I uploaded a version in the market. Now i want to add some new data in the database and uploaded a new version. If user update the app from the market i want to keep the user data(from previous version) and add the new data as well. I have done some google and found that on upgrade method should do the trick. But I am changing the DATABASE_VERSION from code but the on upgrade method does not get call. I am wondering i have missed something. Here is my code:

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/riskycoder.login/databases/";
public static String DB_NAME = "datenbank_EN.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final int DATABASE_VERSION = 1;





public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getWritableDatabase();
        try {
            this.close();
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (checkDB != null)
        checkDB.close();
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[2048];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

    //myDataBase.setVersion(DATABASE_VERSION);
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    Log.d("Test", "Database version: " +myDataBase.getVersion());
}

@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}



@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.d("Test", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);


}

}

like image 455
rawcoder064 Avatar asked May 19 '12 11:05

rawcoder064


1 Answers

onUpgrade() is only called if a change in the database version number is detected. Increment the database version as follows:

private static final int DATABASE_VERSION = 2;
like image 116
Alex Lockwood Avatar answered Oct 13 '22 02:10

Alex Lockwood