Updating the database doesn't work -Android
I wrote some code to ensure that my database will be updated properly when I will release updates to my application.
The problem is that the OnUpdate() function of the SQLiteOpenHelper is never called. Here is the code I wrote in the main activity -
SharedPreferences DB_ver = getSharedPreferences(PREFS_NAME, 0);
myDbHelper = new DataBaseHelper(con, DB_ver.getInt("DB_ver", 1));
try {
if(DB_ver.getInt("DB_ver", 1) !=getPackageManager().getPackageInfo(getPackageName(), 0).versionCode )
{
SharedPreferences.Editor editor = DB_ver.edit();
editor.putInt("DB_ver", getPackageManager().getPackageInfo(getPackageName(), 0).versionCode);
}
} catch (Na开发者_高级运维meNotFoundException e) {
e.printStackTrace();
}
Here is the constructor of SQLiteOpenHelper(which extends SQLiteOpenHelper) -
public DataBaseHelper(Context context,int ver_code) {
super(context, DB_NAME, null, ver_code);
this.myContext = context;
}
Now I understood that the Super line is supposed to call the onUpgrade() function automatically, but it doesn't.
I've tested the function onUpgrade() separately, and it works.
Does anyone know what's the problem?
Thanks!
What your doing is really not neccessary. SQLiteOpenHelper does everything you need. Here's a possible scenario. SQLiteOpenHelper has a getVersion() method in case you need to query it at one point (I never did):
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String dbname = "whatever";
private static final int dbversion = 1; // your first version
//private static final int dbversion = 2; // your second version
//private static final int dbversion = 3; // your third version
public MySQLiteOpenHelper(Context context) {
super(context, dbname, null, dbversion);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqliteDatabase) {
// ... Create first database content
}
@Override
public void onUpgrade(SQLiteDatabase sqliteDatabase, int oldVersion, int newVersion) {
switch (newVersion) {
case dbversion: // suppose your on third version
if (oldVersion == 1) {
upgradeFrom1To2(sqliteDatabase);
upgradeFrom2To3(sqliteDatabase);
}
if (oldVersion == 2) {
upgradeFrom2To3(sqliteDatabase);
}
break;
default:
break;
}
}
public void upgradeFrom1To2(SQLiteDatabase sqliteDatabase) {
// ...
}
public void upgradeFrom2To3(SQLiteDatabase sqliteDatabase) {
// ...
}
}
Two things:
- You're not calling
editor.commit()
. - You're creating the database with an initial version value of 1 in that code. Unless you're changing the version number in the AndroidManifest.xml it will never be anything but 1. Until that version changes
onUpgrade()
doesn't need to be called.onCreate()
will be called when the database is first created, butonUpgrade()
is only called if the reported version becomes different.
You should change the integer "VERSION" to get your onUpgrade called.
Also, the onUpgrade receive two integers, the first one, is the current version of the database(upgrading from), the second is the version you are upgrading to.
One thing I see is that you're not commiting your changes to the SharedPreferences that you're opening. You need to call editor.commit(); to save changes to SharedPreferences.
Also, have you tried actually opening the database in either read or write mode?
精彩评论