1

我正在尝试为 Ubuntu Touch 升级应用程序中的数据库。我使用 QtQuick.LocalStorage 2.0。

当我调用 db.changeVersion 时,它可以工作,但 db.version 在应用重新启动之前不会改变。

db.changeVersion(db.version, "2", function(tx){...}); // Update database to version 2
console.log(db.version); //Should return "2", instead returns previous version of database

如何在不重新启动应用程序的情况下拥有新的 db.version?

4

2 回答 2

1

这最近在 Qt 中得到了修复。changeVersion 函数现在将返回一个更新了版本字段的新数据库对象。

https://bugreports.qt.io/browse/QTBUG-71838

以前的 Qt 版本要求用户重新打开数据库才能看到更新的版本,如测试代码所示:

http://code.qt.io/cgit/qt/qtdeclarative.git/tree/tests/auto/qml/qqmlsqldatabase/data/changeversion.js

于 2018-11-30T13:38:47.513 回答
0

此错误已修复,请参阅 Andrzej 的回答

原始答案

我解决了编写此函数的问题,该函数允许从任何版本的数据库升级到最新版本而不会出现问题。

/* We need this function because db.version is in the .ini file, that is update only by Javascript Garbage Collection.
 * So, we have to upgrade from actual version to the last version using only once db.changeVersion.
 * To avoid to have a lot of switch and spaghetti-code, this function allow to add new db version without modify old sql
 *
 * IMPORTANT: NUMBER OF VERSION HAVE TO BE INT (e.g. 0.1, not 0.1.1)
 */
function upgradeDB() {
    // This is the array with all the sql code, insert your update at the last
    var sqlcode = [
        'CREATE TABLE IF NOT EXISTS Calculations(id INTEGER PRIMARY KEY, calc TEXT)',
        'ALTER TABLE Calculations ADD insertDate INTEGER NOT NULL DEFAULT 0'
    ]

    // This is the last version of the DB, remember to update when you insert a new version
    var lastVersion = "0.2";

    // Hack for change old numeration with new one
    if (db.version == "0.1.1") {
        db.changeVersion("0.1.1", "0.2");
        console.log("Fixed DB!");
    }

    // So, let's start the version change...
    db.changeVersion(db.version, lastVersion,
        function(tx) {
            if (db.version < 0.1) {
                tx.executeSql(sqlcode[0]);
                console.log('Database upgraded to 0.1');
            }
            if (db.version < 0.2) {
                tx.executeSql(sqlcode[1]);
                console.log('Database upgraded to 0.2');
            }

            /* This is the structure of the update:
             * n is the number of version that sql update to
             * m is the number of the sql element in the array. Remember that the number of the first element of array is 0 ;)
            if (db.version < n) {
                tx.executeSql(sqlcode[m]);
                console.log('Database upgraded to n');
            }
            */
        }); // Finish db.changeVersion
}
于 2013-08-10T11:12:44.953 回答