Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to upgrade a DB in an app. I use QtQuick.LocalStorage 2.0.

When I call db.changeVersion it works, but db.version doesn't change until app restarts.

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

How can have the new db.version without restart the app?

share|improve this question
And this is concerning Ubuntu? I see the ubuntu-touch tag, but I think this is better in Stack Overflow. – Braiam Aug 1 '13 at 22:12
Sure it is. This is an Ubuntu SDK question. That the answer might also be relevant on other platforms does not mean it should not be asked about the Ubuntu SDK. – sil Aug 1 '13 at 23:02
@WebbyIT from what you are saying, my understanding is that db.changeVersion is working as expected (i.e. the function documentation does not say that the database version gets updated). Is your callback not doing the version update? I think it might be useful to post what your function is doing to find out what's going on. – David Planella Aug 8 '13 at 11:12
@David Planella well, db.changeVersion changes db.version (that is in the .ini file) only when the app is closed, so you can't use more than one db.changeVersion for session of the app. If you have more than one version of the app (e.g 1.0, 2.0, 3.0) and you want to write db.changeVersion to update to last version, you have to do a function where you can choose if you start from v1.0 or 2.0. To avoid to have a lot of change to do for every new version, I have elaborate this code:… – rpadovani Aug 8 '13 at 12:46
@WebbyIT, ah, gotcha. You might want to add the code snippet as the answer, so that others can benefit from it. – David Planella Aug 9 '13 at 11:31
up vote 0 down vote accepted

I resolved my problem writing this function, that permits to upgrade from any version of the DB to the last version without problems.

/* 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
function upgradeDB() {
    // This is the array with all the sql code, insert your update at the last
    var sqlcode = [
        '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) {
                console.log('Database upgraded to 0.1');
            if (db.version < 0.2) {
                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) {
                console.log('Database upgraded to n');
        }); // Finish db.changeVersion
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.