How to initialize SQLite file for Firefox add-on?

霸气de小男生 提交于 2019-12-08 05:13:21

问题


  1. Is it possible for an SQLite data file used by an add-on to be one of the files accessed with data.url()?

  2. If so, how does one hand it off to 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?


回答1:


Is it possible for an SQLite data file used by an add-on to be one of the files accessed with 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). SQLite needs a physical file however, not something inside an archive.

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?

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




回答2:


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


来源:https://stackoverflow.com/questions/9986740/how-to-initialize-sqlite-file-for-firefox-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!