【问题标题】:Web sql database earased when users update app from App store当用户从 App Store 更新应用程序时,Web sql 数据库被擦除
【发布时间】:2017-07-10 13:23:43
【问题描述】:

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

我什至无法更改表格,因为我不知道用户是否已经拥有表格。目前我正在使用“CREATE TABLE IF NOT EXISTS”来创建表。

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

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

【问题讨论】:

    标签: cordova web-sql


    【解决方案1】:

    看到这个问题没有答案,我很惊讶。所以我发现了一些东西,这就是你的做法,

    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...");
      });
    }
    

    这对我有用!

    【解决方案2】:

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

    这些方法必须立即返回,然后异步运行 以事务回调为第一个的事务步骤 论据

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

    /* 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)');
      });
    }

    【讨论】:

      猜你喜欢
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      相关资源
      最近更新 更多