firefox扩展SQLite保存和获取

发布于 2024-10-21 01:22:15 字数 1458 浏览 0 评论 0原文

好吧,现在来说说多汁的东西。到目前为止,所有尝试都未能保存我的字符串。

以下是将其保存在 Firefox 扩展中的 sqllite 中的代码:

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();

以及用于检索值的代码:

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);

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

mDBConn.drop();

不起作用。有人知道为什么吗? “执行”可以吗?还是我需要“createStatement”或“executeSimpleSQL”。我很困惑。

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_name.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2024-10-28 01:22:15

使用executeSimpleSQL。

openDatabase 返回一个 mozIStorageConnection 实例,该实例没有任何名为 <代码>执行。您可以在任何时候想要执行没有绑定参数的 SQL 语句时使用 executeSimpleSQL (这就是你正在做的)。

您可能正在考虑 mozIStorageStatement execute 方法。当需要绑定参数时,executeSimpleSQL 是不够的。相反,您需要创建一个语句,绑定任何参数,然后然后执行它:

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

另请注意,mozIStorageConnection没有任何名为drop的方法。也许您想编写mDBConn.close()

所有这些都在这里:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文