SQLITE - 设置新数据库的版本

时间:2011-10-04 12:49:00

标签: android sqlite

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

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

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

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

我已设置此行

private static final int DATABASE_VERSION = 5;

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

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

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

这里的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++;
        }
}

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

由于

3 个答案:

答案 0 :(得分:5)

这是正确的:

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

我在getWritableDatabase()下面放了源代码,如你所见,它不会调用onUpgrade(),除非db文件中的版本不等于你在构造函数中传递的当前新版本({{ 1}}行)。因此,您的数据库文件不会被覆盖,或者新数据库中的版本号错误。请检查一下。

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();
        }
    }
}

在您的情况下应该返回PRAGMA user_version;

答案 1 :(得分:0)

它有一个叫做的方法 onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){      在这里执行升级的东西。 。 。 }

如果您使用的是SQLiteOpenHelper类,则只能覆盖此方法。

答案 2 :(得分:0)

只需创建一个

@Override
    public void onCreate(SQLiteDatabase db) {
        onUpgrade(db, 0, DATABASE_VERSION);