简体   繁体   English

如何初始化Firefox附加组件的SQLite文件?

[英]How to initialize SQLite file for Firefox add-on?

  1. Is it possible for an SQLite data file used by an add-on to be one of the files accessed with data.url()? 附加组件使用的SQLite数据文件是否可能是使用data.url()访问的文件之一?

  2. If so, how does one hand it off to Services.storage.openDatabase() 如果是这样,如何将其交给Services.storage.openDatabase()

  3. If not, is it possible for certain code (CREATE TABLE IF EXISTS...) to be executed only in a first-time run of an add-on? 如果没有,某些代码(CREATE TABLE IF EXISTS ...)是否可能仅在首次运行的附加组件中执行?

Is it possible for an SQLite data file used by an add-on to be one of the files accessed with data.url()? 附加组件使用的SQLite数据文件是否可能是使用data.url()访问的文件之一?

No. As of Add-on SDK 1.5, extensions are no longer uncompressed upon installation - they stay as packed XPI files on disk (which is good for performance). 从Add-on SDK 1.5开始,扩展在安装时不再解压缩 - 它们作为打包的XPI文件保留在磁盘上(这有利于提高性能)。 SQLite needs a physical file however, not something inside an archive. SQLite需要一个物理文件,而不是存档中的东西。

If not, is it possible for certain code (CREATE TABLE IF EXISTS...) to be executed only in a first-time run of an add-on? 如果没有,某些代码(CREATE TABLE IF EXISTS ...)是否可能仅在首次运行的附加组件中执行?

Sure but you shouldn't do it like this - what if your database file gets deleted for some reason? 当然,你不应该这样做 - 如果你的数据库文件由于某种原因被删除怎么办? It is better to check whether the database already exists: 最好检查数据库是否已存在:

var dbFile = FileUtils.getFile("ProfD", "foobar.sqlite");
var alreadyExists = dbFile.exists();
var dbConnection = Services.storage.openDatabase(dbFile);
if (!alreadyExists)
  connection.createTable("foo", "id INTEGER PRIMARY KEY, ...");

For reference: FileUtils.jsm 供参考: FileUtils.jsm

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

let file = FileUtils.getFile("ProfD", ["my_db_file_name.sqlite"]);
let mDBConn = Services.storage.openDatabase(file); // Will also create the file if it does not exist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM