当用户从 App store 更新应用程序时,网络 sql 数据库被清空

Web sql database earased when users update app from App store

我有一个使用网络 sql 数据库的 cordova 应用程序。在发布此版本之前,我更新了 table 以包含到新列中。当用户更新应用程序时,以前的数据将被删除。如果用户安装一个新副本,它可以工作,并且在更新用户时无法找到任何以前的数据。

我什至无法更改 table,因为我不知道用户是否已经拥有 table。目前我正在使用 'CREATE TABLE IF NOT EXISTS' 创建一个 table.

var myDB = openDatabase('test', 1.0, 'Native APP', 5*1024*1024);

我尝试将数据库的版本从 1.0 更改为 2.0,但无济于事。需要一些帮助!

我很惊讶没有看到这个问题的答案question.So我发现了一些东西这是你如何做的,

var db = openDatabase("example_db", "", "Example Database", 100000);

if(db.version == ""){
  db.changeVersion("", "1", function(t){
    t.executeSql("create table foo...");
  }, null /* error callback */, function(){
    // success callback
    db.changeVersion("1", "2", function(t){
      t.executeSql("alter table foo...");
    });    
  });
}

if(db.version == "1"){
  db.changeVersion("1", "2", function(t){
    t.executeSql("alter table foo...");
  });
}

这对我有用!

来自文档:W3.org Web SQL Database。 由于 changeVersion 是一个异步方法并且

these methods must immediately return and then asynchronously run the transaction steps with the transaction callback being the first argument

,必须仔细评估检查的顺序。

/* the version is used only if the callbackfunction is not passed. In this case we pass it and therefore we have to create it with changeversion */
var db = openDatabase('documents', '1.0', 'Offline document storage', 5*1024*1024, 
  function (db) {//Enter only the creation of the database, the first time
    db.changeVersion('', '1.0', function (t) {
      t.executeSql('CREATE TABLE docids (id, name)');
    }, error);
  }
);
/*To make changes to the database, it will be necessary to slowly add the functions thus written*/

if(db.version == "1.0") {
  db.changeVersion("1.0", "1.2", 
 function(trans) {
  //All functions from 1.0 to 1.2
  trans.executeSql('CREATE TABLE IF NOT EXISTS DEMO1 (id unique, data, foo)');
        trans.executeSql('CREATE TABLE IF NOT EXISTS DEMO2 (id unique, data, foo)');
        trans.executeSql('CREATE TABLE IF NOT EXISTS DEMO3 (id unique, data, foo)');
  });
}
else if(db.version == "1.1") {
    db.changeVersion("1.1", "1.2", 
 function(trans) {
  //Only functions from 1.1 to 1.2
        trans.executeSql('CREATE TABLE IF NOT EXISTS DEMO2 (id unique, data, foo)');
        trans.executeSql('CREATE TABLE IF NOT EXISTS DEMO3 (id unique, data, foo)');
  });
}