Lori
Lori

Reputation: 1422

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

  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?

Upvotes: 2

Views: 1404

Answers (2)

noit
noit

Reputation: 1

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

Upvotes: -2

Wladimir Palant
Wladimir Palant

Reputation: 57651

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

Upvotes: 6

Related Questions