开发者

firefox extension SQLite saving and getting

Ok now for the juicy stuff. All attempts failed to save my string so far.

Here is the code for saving it in sqllite in firefox extension:

var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
    file.append("my_db_file_name.sqlite");

    var storageService = Components.classes["@mozilla.org/storage/service;1"]
                                   .getService(Components.interfaces.mozIStorageService);

    var mDBConn = storageService.openDatabase(file);

    mDBConn.execute("CREATE TABLE IF NOT EXISTS log_det (id INTEGER PRIMARY KEY AUTOINCREMENT, acc STRING)");
    mDBConn.execute("INSERT INTO log_det (acc) VALUES(" + window['gluistr']+ ")");
    mDBConn.drop();

And the code for retrieving the value:

var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);

file.append("my_db_file_n开发者_开发问答ame.sqlite");

var storageService = Components.classes["@mozilla.org/storage/service;1"]
                                   .getService(Components.interfaces.mozIStorageService);

var mDBConn = storageService.openDatabase(file);

var res = mDBConn.execute("SELECT * FROM log_det");

mDBConn.drop();

Is not working. Anybody knows why? Is "execute" ok or do I need "createStatement" or "executeSimpleSQL". I am confused.


Use executeSimpleSQL.

openDatabase returns a mozIStorageConnection instance, which does not have any method named execute. You can use executeSimpleSQL any time you want to execute a SQL statement without bound parameters (which is what you're doing).

You were probably thinking of mozIStorageStatement's execute method. executeSimpleSQL is not sufficient when bound parameters are necessary. Instead, you need to create a statement, bind any parameters, and then execute it:

var statement = mDBConn.createStatement(
    "SELECT * FROM log_det WHERE column_name = :parameter");
statement.bindStringParameter(0, "value");
statement.execute();
statement.reset();

Also note that mozIStorageConnection does not have any method named drop. Maybe you meant to write mDBConn.close()?

All of this is covered here:

  • https://developer.mozilla.org/en/storage
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜