1

I'm trying to create a bookmark extension in Chrome and I want to leverage WebSQL to store all kind of information about bookmarks locally. Here's what I've done so far:

(function() {
  var Home,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;

  Home = (function() {

    function Home() {
      this.onDBInit = __bind(this.onDBInit, this);      this.db = openDatabase("Journal", "", "Bookmarks Stats", 5 * 1024 * 1024, this.onDBInit, this.onDBError);
    }

    Home.prototype.onDBInit = function(db) {
      console.log(db.version);
      db.changeVersion("", "1.0", this.initDB, this.onDBError);
      return console.log(db);
    };

    Home.prototype.initDB = function(t) {
      console.log(t);
      return t.executeSql('CREATE TABLE bookmarks (id, title, url)');
    };

    Home.prototype.onDBError = function(e) {
      return console.log(e);
    };

    return Home;

  })();

  window.Registerable(Home);

}).call(this);

For some reason, changeVersion ALWAYS fails. I have tried to delete the database, restart chrome, etc. Chrome version: 18.

4

2 に答える 2

0

In my limited experience, changeVersion does work on Chrome. I've also read complaints here about it not working properly on Safari, but it does.

However, there are two catches:

Catch 1:

Often, changeVersion appears to fail (it gives an error and db.version will still return the old value), but the transaction callback will fire and when you re-open the web page and its database, the version number will be correct.

Catch 2:

It seems you must supply precisely five arguments, including three callbacks. If you don't supply these five arguments, for example you only do the first three, then the current version will remain unchanged. So if you were to follow the instruction in this tutorial that everyone's referring to, you would be disappointed.

This is the case for Chrome and Safari on Windows.

Arguments are: 1: expected version 2: new version 3: transaction callback (you can execute SQL here as part of the version change) 4: failure callback (if version change or transaction callback failed) 5: succes callback (if version change and transaction callback succeeded)

I haven’t tested this on iOS devices but it matches the Safari specifications provided here: https://developer.apple.com/library/safari/#documentation/iphone/conceptual/safarijsdatabaseguide/usingthejavascriptdatabase/usingthejavascriptdatabase.html

In Opera, changeVersion waits with setting db.version to its new value until after you've executed an actual sql transaction using executeSql. So I use a SELECT statement that has no further consequence. This does not actually have to be inside the transaction callback: I discovered it when I tried a separate executeSql statement in the browser console after trying a whole bunch of changeVersion commands.

于 2012-06-21T10:24:13.203 に答える
0

This is Chrome bug: you can not change the version of the database with an empty string in the value.

And according to the specifications in the call creationCallback (in your case, the function onDBInit) version of the database is exposed to an empty string:

the callback is invoked with the database having the empty string as its version regardless of the given database version

This error is detected two years ago and described in detail, but is still not corrected

I have decided this issue in the following ways: I removed the initialization of the database structure of creationCallback and made it the first transaction to the database.

var db = openDatabase("Journal", "0.1", "Bookmarks Stats", 5 * 1024 * 1024);
db.transaction(function (t) {
    t.executeSql('CREATE TABLE IF NOT EXISTS bookmarks (id, title, url)');
});
于 2015-08-07T03:53:08.757 に答える