SQLite onUpgrade未调用

时间:2018-07-31 16:50:15

标签: android android-sqlite

我正在检查应用程序的更新过程,并且未调用onUpgrade。 我确实知道getWritableDatabase();或  getReadableDatabase(); 应该执行以便调用onUpgrade并应该增加DB_VERSION。

private DBHelper(Context context) {

    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}

我已在模拟器中安装了该应用程序的当前发行版并执行:

pragma user_version;

由于某种原因,它返回 0 ,尽管该源代码的数据库版本为 13 。 在新版本中,我已将数据库版本更新为20,以进行测试,安装更新后,使用 adb install -r myapp.apk 数据库版本为20(使用相同的pragma user_version;),但未调用onUpgrade。

此过程通常非常简单,所以我不确定出了什么问题。

有人知道出什么问题了吗?为什么当前版本的user_version为0,为什么不调用onUpgrade?

这是DBHelper类:

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

    }
}

1 个答案:

答案 0 :(得分:1)

检查source code of SQLiteOpenHelper后,我部分理解了这个问题,并且@slackwars的假设已被证实。

似乎仅当user_version!= 0时才调用onUpgrade。

if (version != mNewVersion){
 ...

 if (version == 0) {
     onCreate(db);
 } else {
        if (version > mNewVersion) {
            onDowngrade(db, version, mNewVersion);
        } else {
            onUpgrade(db, version, mNewVersion);
        }
 }
 db.setVersion(mNewVersion);

我不确定为什么它为0,但是我已经使用它来创建一个使用onCreate的解决方法并解决此问题。