SQLITE - 设置新数据库的版本

Jam*_*ley 7 sqlite android

我已经使用此示例创建了一个数据库,我在全新安装时复制了该数据库

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

我现在遇到的问题是我现在已对数据库进行了一些更新,并且我已经对构建中的主文件进行了此更新,因此用户始终在新安装时获得最新版本

我有一个不同的类来处理所有db调用查询语句.

我已经设置了这一行

private static final int DATABASE_VERSION = 5;
Run Code Online (Sandbox Code Playgroud)

在全新安装时,数据库被正确复制,但当查询类再次调用databasehelper时,调用onupgrade()方法并尝试更新到最新版本,并且应用程序崩溃,因为它尝试执行无法完成的升级

我的理解是,以下设置新安装的数据库版本或者这是错误的.如果是,如何为新安装设置数据库版本

public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        this.context = context;
    }
Run Code Online (Sandbox Code Playgroud)

这里只是for completness是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++;
        }
}
Run Code Online (Sandbox Code Playgroud)

询问全新安装时新数据库设置的版本,如果不是版本5,如何覆盖它

谢谢

Can*_*ner 5

这是对的:

public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        /* ... */
}
Run Code Online (Sandbox Code Playgroud)

我把下面的getWritableDatabase()源代码放在下面,并且您看到它不会调用,onUpgrade()除非db文件中的版本不等于您在Constructor(version != mNewVersion行)中传递的当前新版本。因此,要么数据库文件未被覆盖,要么新数据库中的版本号错误。请检查一下。

/**
 * 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑
您可以使用以下查询在新数据库文件中检查版本:

PRAGMA user_version;
Run Code Online (Sandbox Code Playgroud)

它应该返回5您的情况。

  • 如果没有得到5,则执行以下操作:PRAGMA user_version = 5; (2认同)