ORMlite数据库版本在onUpgrade之后不递增

时间:2014-01-31 17:07:38

标签: android ormlite

我一直在研究Android项目,而且我一直在使用ORMLite。到目前为止,我已经能够做我需要的一切,现在我正在使用一个非常复杂的数据模型。

现在看来我遇到了帮助器的onUpgrade方法的问题。

以下是我的onUpgrade方法的基本要点:

public void onUpgrade(final SQLiteDatabase db, final ConnectionSource connectionSource, int oldVersion, final int newVersion) {
try {
    // Simply loop round until newest version has been reached and add the appropriate migration
    while (++oldVersion <= newVersion) {
        // Get all the available update SQL statements and execute them
        final String[] availableUpdates = SQLStaticContentHelper.getMigrationStatements(oldVersion);
        executeStatements(db, availableUpdates, true);
    }           
}
catch (Exception e) {
    Log.e(LOG_TAG, "onUpgrade() - Can't migrate databases, bootstrap database, data will be lost", e);
    onCreate(db, connectionSource);
}

这是我的onCreate方法:

public void onCreate(SQLiteDatabase database ,ConnectionSource connectionSource) {    
try {
    //create the tables
    for (Class<?> c : allTablesArray)
    {
        try {
            TableUtils.dropTable(connectionSource, c, false);
        } catch (SQLException e) {Log.d(LOG_TAG, "onCreate() - Not droping table " + c.getCanonicalName() + ". Does not exist.");}
        TableUtils.createTable(connectionSource, c);
    }

} catch (Exception e) {
    Log.e(LOG_TAG, "onCreate() - Problem creating tables : ", e);
    throw new RuntimeException(e);
} 

预期的行为如下:

  1. 有升级
  2. 升级
  3. 如果资源数据库升级不成功
  4. 增量版
  5. 我实际上进行了升级,一切正常(当它工作正常时)。但在我的测试中,我故意插入一个SQL错误,它确实转到onCreate并删除表并创建表,但它不会增加版本(下次重启应用程序时它会尝试再次升级)。

    任何人都知道为什么会这样?如果onUpgrade中没有发生异常,则数据库版本会正确更新。

0 个答案:

没有答案
相关问题