设置一个新的数据库版本

设置一个新的数据库版本

本文介绍了SQLITE - 设置一个新的数据库版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个例子来创造,我复制了一个新的安装数据库

I have used this example to create a database that I copy over on a fresh install

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

问题我现在已经是我现在已经做了一些更新到数据库和我asummed我也做这个更新的版本中的主文件,使用户始终获得最更新的版本,新的安装

Problem I have now is I have now done some updates to the database and I asummed I also do this updates to the master file within the build so the users always get the most update version on new install

我有一个不同的类,它与所有的数据库调用查询语句处理。

I have a different class that deals with all the db calls query statements.

我已经设置这一行

private static final int DATABASE_VERSION = 5;

在一个新的安装,现在数据库被正确地复制,但是当查询类调用databasehelper再次onupgrade()方法被调用并尝试更新到最新版本的应用程序崩溃,因为它试图做的升级能不能做到

On a fresh install now the database is copied over correctly but when the query class calls the databasehelper again the onupgrade() method is called and tries to update to the latest version and the app crashes as it is trying to do upgrades that cannot be done

我下的是理解,以下一组执行全新安装的数据库版本,或者这是错误的。如果是的话我怎么设置数据库版本新安装

I was under the understanding that the following set the database version on fresh installs or is this wrong. If so how do I set the database version for new installs

public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

只是completness这里是onupgrade样本()

just for completness here is a sample of the onupgrade()

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

        dbUpgrade = new DatabaseUpgrades(context);

        Log.d(DEBUG_TAG, "Calling onupgrade db");
        Log.d(DEBUG_TAG + " : " + DatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                    + newVersion);

        if (oldVersion == 1) {
            Log.d(DEBUG_TAG, "Updating to Version 2");
            dbUpgrade.upgradeDB2(db);
            oldVersion++;
        }
}

问什么版本的新的数据库设置为在全新安装以及如何覆盖它,如果它不是第5版

Question what version is the new database set to on a fresh install and how can you overwrite it if it is not version 5

感谢

推荐答案

这是正确的:

public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        /* ... */
}

我把下面的 getWritableDatabase()源$ C ​​$ C,当你看到它不会叫 onUpgrade()除非数据库文件的版本不等于电流传递在构造新的版本(版本!= mNewVersion 行)。因此,无论你的数据库文件不会被覆盖或在您的新数据库的版本号是错误的。请检查一下。

I put below getWritableDatabase() source code and as you see it won't call onUpgrade() unless the version in the db file is not equal to current new version which you pass in the constructor(version != mNewVersion line). So either your database file is not overwritten or version number in your new database is wrong. Please check it.

/**
 * Create and/or open a database that will be used for reading and writing.
 * Once opened successfully, the database is cached, so you can call this
 * method every time you need to write to the database.  Make sure to call
 * {@link #close} when you no longer need it.
 *
 * <p>Errors such as bad permissions or a full disk may cause this operation
 * to fail, but future attempts may succeed if the problem is fixed.</p>
 *
 * @throws SQLiteException if the database cannot be opened for writing
 * @return a read/write database object valid until {@link #close} is called
 */
public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
        return mDatabase;  // The database is already open for business
    }

    if (mIsInitializing) {
        throw new IllegalStateException("getWritableDatabase called recursively");
    }

    // If we have a read-only database open, someone could be using it
    // (though they shouldn't), which would cause a lock to be held on
    // the file, and our attempts to open the database read-write would
    // fail waiting for the file lock.  To prevent that, we acquire the
    // lock on the read-only database, which shuts out other users.

    boolean success = false;
    SQLiteDatabase db = null;
    if (mDatabase != null) mDatabase.lock();
    try {
        mIsInitializing = true;
        if (mName == null) {
            db = SQLiteDatabase.create(null);
        } else {
            db = mContext.openOrCreateDatabase(mName, 0, mFactory);
        }

        int version = db.getVersion();
        if (version != mNewVersion) {
            db.beginTransaction();
            try {
                if (version == 0) {
                    onCreate(db);
                } else {
                    onUpgrade(db, version, mNewVersion);
                }
                db.setVersion(mNewVersion);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        onOpen(db);
        success = true;
        return db;
    } finally {
        mIsInitializing = false;
        if (success) {
            if (mDatabase != null) {
                try { mDatabase.close(); } catch (Exception e) { }
                mDatabase.unlock();
            }
            mDatabase = db;
        } else {
            if (mDatabase != null) mDatabase.unlock();
            if (db != null) db.close();
        }
    }
}

修改
您可以检查版本在新的数据库文件,下面的查询:

EDIT
You can check version in your new database file with the following query:

PRAGMA user_version;

这应该返回 5 你的情况。

这篇关于SQLITE - 设置一个新的数据库版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:24