javascript - 在 Thunderbird 中查询 SQLite 数据库

标签 javascript sqlite thunderbird thunderbird-addon

我想为 Thunderbird 制作一个使用 SQLite 数据库的小型插件。我用“SQLite Manager”创建了我的数据库。 问题是,当我访问数据库时,我收到数据库为空的消息。但事实并非如此!

数据库:

----------------------------
| table1 | table2 | table3 |
----------------------------
|  ...   |  ...   |  ...   |
|  ...   |  ...   |  ...   |
----------------------------

这是 JavaScript 代码。它应该返回表的名称,但我得到 0。

  Components.utils.import("resource://gre/modules/Sqlite.jsm");
  Components.utils.import("resource://gre/modules/Task.jsm");

  Task.spawn(function() {
    let db;
    let result;
    try {

      db = yield Sqlite.openConnection({ path: "db.sqlite" });
      result = yield db.execute("SELECT name FROM sqlite_master WHERE type='table'");

      alert(result.length);

    } catch (ex) {
      alert("error: " + ex);
    } finally {
      if (db) yield db.close();
    }
  });

谁能告诉我我做错了什么?

是否可以导入并读取 Thunderbird 中已有的数据库?

谢谢!

最佳答案

查看source code for Sqlite.jsm ,路径始终相对于配置文件。但是,您可以访问模块的后台 channel 并从 openConnection 复制/粘贴代码。这是保存模块中可用的未导出符号的对象。像这样:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;

function openConnection2(path) {
  let Sqlite = Components.utils.import("resource://gre/modules/Sqlite.jsm", {});
  let file = FileUtils.File(path);
  let identifier = Sqlite.getIdentifierByPath(path);
  let openedOptions = {};

  return new Promise((resolve, reject) => {
    let dbOptions = null;
    Services.storage.openAsyncDatabase(file, dbOptions, (status, connection) => {
      if (!connection) {

        reject(new Error(`Could not open connection to ${path}: ${status}`));
        return;
      }
      log.info("Connection opened");
      try {
        resolve(
          new Sqlite.OpenedConnection(connection.QueryInterface(Ci.mozIStorageAsyncConnection),
                                      identifier, openedOptions));
      } catch (ex) {
        reject(ex);
      }
    });
  });
}

你可以在这里传递你自己的路径,这应该是完整路径。您可以通过将 chrome uri 转换为文件 uri 来访问数据库:

let uri = Services.io.newURI("chrome://myext/content/db.sqlite", null, null);
let registry = Cc['@mozilla.org/chrome/chrome-registry;1']
                .getService(Ci.nsIChromeRegistry);
let path = registry.convertChromeURL(uri)
                   .QueryInterface(Ci.nsIFileURL)
                   .file.path;

yield openConnection2(path); // in your task

请注意,您需要在 install.rdf 中指定 em:unpack 以确保转换后的 uri 实际上是文件 uri,而不是 jar: uri。

关于javascript - 在 Thunderbird 中查询 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227200/

相关文章:

javascript - jQuery 自动完成返回整个数组而不是搜索值

javascript - 更改顶点图中单个条形的颜色

javascript - 如何确定给定字符串是否代表日期?

sqlite - SQLite 中的儒略日到 ISO 8601 字符串

c++ - 如何在qt中修复 "No query Unable to fetch row"

sqlite - 找不到参数的方法 compile() [项目 ':react-native-sqlite']

javascript - phonegap 页面重定向不起作用,对象的属性 'location' 不是函数

xul - 雷鸟扩展 : add button in message reader toolbar

php - UTF-8 纠正后出现奇怪的编码问题 "most"

delphi - Thunderbird 是否始终作为 MAPI 邮件客户端工作?