cordovaweb-sql

Web sql database earased when users update app from App store


I have a cordova application which uses web sql databse. Before releasing this version I have updated a table to include to new columns. When user update the APP previous data is erased. If user install a new copy it works and on updating user is unable to find any previous data.

I can't even alter the table because I don't know if user already has table or not. Currently I am using 'CREATE TABLE IF NOT EXISTS' to create a table.

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

I have tried changing the version of the database from 1.0 to 2.0 but to no avail. Need some help!


Solution

  • I am surprised to see no answers to this question.So I found something Here is How you do it,

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

    This worked for me!