Search code examples
javascriptsqlitestoragefirefox-addon-sdk

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?


Solution

  • 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