2013-08-05 6 views
1

저는 Ubuntu Touch 용 응용 프로그램에서 DB를 업그레이드하려고합니다. QtQuick.LocalStorage 2.0을 사용합니다.db.changeVersion이 예상대로 작동하지 않습니다.

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을 만들 수 있습니까?

답변

0

문제없이 DB의 모든 버전에서 마지막 버전으로 업그레이드 할 수 있도록이 기능을 작성하는 데 문제가 발생했습니다.

/* 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 
}