成功升级后的And​​r​​oid升级数据库没有更新版本分贝 [英] Android upgrade db not updating db version after successful upgrade

查看:166
本文介绍了成功升级后的And​​r​​oid升级数据库没有更新版本分贝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想三个新表添加到现有的SQLite数据库和我正在与成功升级后,DB版本没有更新的问题。下面是运行DatabaseHelper:

I am trying to add three new table to my existing sqlite db and I'm running into problems with the db version not updating after a successful upgrade. Below is the DatabaseHelper that runs:

private static class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, "Current db version is " + db.getVersion());
        db.execSQL(ORIGINAL_DATABASE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());
        db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_3_DATABASE_CREATE);
        db.setVersion(newVersion);
        Log.w(TAG, "Version after onUpgrade is " + db.getVersion());
    }       
}

下面是我的open()函数:

Below is my open() function:

public VehicleExpDbAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

下面是我的close()函数:

Below is my close() function:

public void close() {
    mDb.close();
    mDbHelper.close();
}

因此​​,对具有较高DATABASE_VERSION第一次运行会发生什么是第一onUpgrade日志中读取出来:
旧版本1新版本2 db.getVersion 1

So what happens on the first run with a higher DATABASE_VERSION is the first onUpgrade Log reads out: Old Version 1 New Version 2 db.getVersion is 1

第二个日志是:
onUpgrade后的版本是2

The second Log is: Version after onUpgrade is 2

但是当分贝再次访问后onUpgrade被跑不更新数据库的版本号,并将其通过onUpgrade与第一日志中的onUpgrade读数运行:
旧版本1新版本2 db.getVersion 1

But then when the db is accessed again after the onUpgrade was ran the version number of the DB is not updated and it runs through the onUpgrade with the first log in the onUpgrade reading: Old Version 1 New Version 2 db.getVersion is 1

然后应用程序崩溃,因为它试图创建一个表已经存在。

Then the app crashed because it tries creating a table that is already there.

我试过没有手动设置在onUpgrade数据库版本。这也不起作用。我也试着通过运行更新的版本号...

I've tried not manually setting the db version in the onUpgrade as well. This didn't work either. I've also tried updating the version number by running...

db.execSQL("PRAGMA user_version = " + newVersion);

...在onUpgrade的末尾。

...at the end of the onUpgrade.

编辑:

每Aswin Kumar的建议,我已经改变了我onUpgrade到备份现有的表并删除所有表,然后重新创建它们。这并没有解决我的版本问题。下面是我的onUpgrade和的onCreate:

Per Aswin Kumar's suggestion I've changed my onUpgrade to backing up my existing table and dropping all table and then recreating them. This has not fix my version issue. Below is my onUpgrade and onCreate:

@Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, "Current db version is " + db.getVersion());

        db.execSQL(ORIGINAL_DATABASE_CREATE);

        db.execSQL(NEW_TABLE_1_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_2_DATABASE_CREATE);
        db.execSQL(NEW_TABLE_3_DATABASE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());

        mOldDbContents = backupTables(db);

        db.execSQL("DROP TABLE IF EXISTS " + ORIGINAL_DATABASE_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_1_DATABASE_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_2_DATABASE_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + NEW_TABLE_3_DATABASE_CREATE);

        onCreate(db);
    }   

下面是我用它来创建表的sqlite的语句:

Below are the sqlite statements that I use to create the tables:

private static final String ORIGINAL_DATABASE_CREATE = "create table " + VEHICLE_EXPENSE_TABLE_NAME + 
        " (" + KEY_ROW_ID + " integer primary key autoincrement, " + 
        KEY_UNIX_DATE + " integer, " + 
        KEY_DATE + " not null default current_date, " +
        KEY_TIME + " not null default current_time, " + 
        KEY_DESCRIPTION + " text, " + 
        KEY_START_MILE + " integer, " + 
        KEY_END_MILE + " integer, " + 
        KEY_MILES + " text, " + 
        KEY_AMOUNT + " text, " 
        + KEY_PARTY_ID + " integer)";

private static final String NEW_TABLE_1_DATABASE_CREATE = "CREATE TABLE " + PURCHASE_HISTORY_TABLE_NAME + 
        "(" + HISTORY_ORDER_ID_COL + " TEXT PRIMARY KEY, " +
        HISTORY_STATE_COL + " INTEGER, " + 
        HISTORY_PRODUCT_ID_COL + " TEXT, " + 
        HISTORY_DEVELOPER_PAYLOAD_COL + " TEXT, " + 
        HISTORY_PURCHASE_TIME_COL + " INTEGER)";

private static final String NEW_TABLE_2_DATABASE_CREATE = "CREATE TABLE " + PURCHASED_ITEMS_TABLE_NAME + 
        "(" + PURCHASED_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " + 
        PURCHASED_QUANTITY_COL + " INTEGER)";

private static final String NEW_TABLE_3_DATABASE_CREATE = "CREATE TABLE " + TRIAL_LIMIT_TABLE_NAME + 
        "(" + TRIAL_PRODUCT_ID_COL + " TEXT PRIMARY KEY, " + 
        TRIAL_PRODUCT_NAME + " TEXT, " + 
        TRIAL_START_DATE + " INTEGER)";

任何帮助将是巨大的AP preciated。

Any help would be great appreciated.

感谢您,
凯文

Thank you, Kevin

推荐答案

固定我自己的问题。问题是,我已经创造了一个访问被试图升级相同的DB单独DB辅助类。在这个单独的DB辅助类的版本号设置为1。这个程序是我的第一个Java和Android程序,并设置我的DB类可怕。我已经合并所有DB类一现在我升级,现在我的数据库版本更新是像它应该。下面是我最后的班如果有人有兴趣。

Fixed my own problem. The issue was that I had created a separate DB helper class that was accessing the same DB that was was trying to upgrade. In this separate DB helper class the version number was set to 1. This app was my first Java and Android program and I setup the DB classes horribly. I've consolidated all the DB class to the one i'm now upgrading and now my DB version is updating like it should. Below are my final classes if anyone is interested.

@Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, "Current db version is " + db.getVersion());

        db.execSQL(VEHICLE_EXPENSE_DATABASE_CREATE);

        db.execSQL(PURCHASE_HISTORY_DATABASE_CREATE);
        db.execSQL(PURCHASE_ITEMS_DATABASE_CREATE);
        db.execSQL(TRIAL_TIME_DATABASE_CREATE);
    }


@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        Log.w(TAG, "Old Version " + oldVersion + " New Version " + newVersion + " db.getVersion is " + db.getVersion());

        if (oldVersion < newVersion) {

            if (oldVersion == 1) {
                db = upgradeTo2(db);
            }
        }
    }

private SQLiteDatabase upgradeTo2(SQLiteDatabase db) {

        db.beginTransaction();
        try {
            db.execSQL(PURCHASE_HISTORY_DATABASE_CREATE);
            db.execSQL(PURCHASE_ITEMS_DATABASE_CREATE);
            db.execSQL(TRIAL_TIME_DATABASE_CREATE);
            db.setTransactionSuccessful();
        } catch (Exception e) {
            Log.w(TAG, "onUpgrade failed");
        } finally {
            db.endTransaction();
        }

        return db;
    }

这篇关于成功升级后的And​​r​​oid升级数据库没有更新版本分贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆