Reputation: 7674
I'm checking the update process of an application and onUpgrade is not called.
I do know that getWritableDatabase();
or
getReadableDatabase();
should be executed in order for onUpgrade to be called and that DB_VERSION should be incremented.
private DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
I've installed the current released version of the app in an emulator and executed:
pragma user_version;
For some reason it returns 0 although the DB version of the source code of this version is 13.
In the new version I've updated the DB version to 20 for testing, and after I install the update, using
adb install -r myapp.apk
the DB version is 20 (using the same pragma user_version;
), but onUpgrade is not called.
This process is usually quite straightforward, so I'm not sure what went wrong.
Does someone have an idea what's wrong? why the user_version of the current version is 0 and why onUpgrade is not called?
This is the DBHelper class:
public class DBHelper extends SQLiteOpenHelper {
private static String DB_NAME = "db_name.db3";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static DBHelper sInstance;
public static int DB_VERSION = 20;
public static DBHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DBHelper(context.getApplicationContext());
}
return sInstance;
}
private DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
public void createDataBase() throws IOException {
try {
boolean dbExist = checkDataBase();
if (dbExist) {
try {
SQLiteDatabase db = this.getWritableDatabase();
} catch (SQLiteException e) {
Crashlytics.logException(e);
}
} else {
SQLiteDatabase db = this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Crashlytics.logException(e);
}
}
} catch (Exception e) {
Crashlytics.logException(e);
}
}
private boolean checkDataBase() {
boolean checkdb = false;
try {
String myPath = myContext.getFilesDir().getAbsolutePath()
.replace("files", "databases")
+ File.separator + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch (SQLiteException e) {
Crashlytics.logException(e);
}
return checkdb;
}
public SQLiteDatabase openDataBase() throws SQLException {
String myPath = myContext.getFilesDir().getAbsolutePath()
.replace("files", "databases")
+ File.separator + DB_NAME;
if (myDataBase == null) {
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} else {
if (myDataBase.isOpen()) {
return myDataBase;
} else {
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
}
return myDataBase;
}
public SQLiteDatabase openDataBaseForWrite() throws SQLException {
String myPath = myContext.getFilesDir().getAbsolutePath()
.replace("files", "databases")
+ File.separator + DB_NAME;
if (myDataBase == null) {
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} else {
if (myDataBase.isOpen()) {
if (myDataBase.isReadOnly()) {
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} else {
return myDataBase;
}
} else {
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
}
return myDataBase;
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//NOT CALLED
}
}
Upvotes: 3
Views: 94
Reputation: 7674
After checking the source code of SQLiteOpenHelper, I partially understand the issue and @slackwars assumption was spot on.
Seems onUpgrade will be called only if user_version != 0.
if (version != mNewVersion){
...
if (version == 0) {
onCreate(db);
} else {
if (version > mNewVersion) {
onDowngrade(db, version, mNewVersion);
} else {
onUpgrade(db, version, mNewVersion);
}
}
db.setVersion(mNewVersion);
I'm not sure why it's 0, but I've used this to create a workaround using onCreate and solve this issue.
Upvotes: 1