2

I am trying to run these set of functions:

function erorr(e) {
  // error getting database
  alert(e.message);
}

window.onload = function() {
    prepareDatabase(erorr);
};

function prepareDatabase(error) {
  return openDatabase('tasks13', '', 'Offline task storage', 3*1024*1024, function (db) {
    db.changeVersion('', '1.0', function (t) {
      t.executeSql('CREATE TABLE tasks (id, detail,status)');
    }, error);
  });
}

But, after running this I get an error current version of the database and 'oldVersion' argument do not match. Not sure what wrong I am doing here.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
M T
  • 4,099
  • 4
  • 21
  • 27

2 Answers2

3

Correct code:

function prepareDatabase(error) {
  return openDatabase('tasks13', '', 'Offline task storage', 3*1024*1024, function (db) {
    db.changeVersion(db.version, '1.0', function (t) {
      t.executeSql('CREATE TABLE tasks (id, detail,status)');
    }, error);
  });
}

While it's possible to open any available version of a WebSQL db (by passing an empty string as version identifier), you need to explicitly specify the current version of the db when calling db.changeVersion. The current version of the db is made available as db.version.

This is what the specification says:

Check that the value of the first argument to the changeVersion() method exactly matches the database's actual version. If it does not, then the preflight operation fails.

From http://www.w3.org/TR/webdatabase/#asynchronous-database-api

Myrne Stol
  • 11,222
  • 4
  • 40
  • 48
-1

I ran into the same error.

I refrained from using db.changeVersion and used the following more imperative style of logic instead:

this.db = window.openDatabase('myDb', '1.0', 'a comment', 5*1024*1024);
if (this.db) {
    this.db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS myTable(...)',
            [],
            function(tx, rs) { },
            function(tx, err) { alert("Error in create table occurred: " + err) }
        );
    });
}

Hope it works for you as well.

/Fredrik

Fredrik
  • 91
  • 3