1

Is there any way to access a SQLite database inside the Data directory in the add-on builder using add-on sdk v1.5?

this is the code that i am using:

var {Cc, Ci, Cu} = require("chrome");
var data = require('self').data;

// This is an active module of the pankajsingh5k Add-on
exports.main = function() {

var {Services} = Cu.import("resource://gre/modules/Services.jsm");
var {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm");

var file = FileUtils.getFile("Desk", "Helper.sqlite");

var mDBConn = Services.storage.openDatabase(file);

console.log('loading');

var statement = mDBConn.createStatement("SELECT * FROM Words");  

right now FileUtils.getFile() is set to "Desk" which stands for Desktop. I dont know how i can access the sql lite file in the add on builder directory structure.

Pankaj Kumar
  • 1,748
  • 6
  • 28
  • 41

1 Answers1

1

You cannot and there is a very simple reason for that - with bug 638742 fixed (starting with Add-on SDK 1.5 I think) the data directory no longer is a directory on disk. The add-on is installed as an XPI file on disk and this directory is actually a directory inside the packed XPI file. SQLite needs a real file to work with, putting changing data inside the data directory would have been a bad idea anyway.

You should use "ProfD" instead of "Desk" - this is the user's profile directory. Make sure to choose a file name that clearly belongs to your extension, all extension are writing data into the profile.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • thanks for the answer and clarifying the details. i will be using "ProfD" instead of "Desk". Since all the words will be stored in the sqlite database beforehand(prepopulated) and i just need to read them is there any way to copy the sqllite database into the profile directory when anyone installs the addon? – Pankaj Kumar Mar 07 '12 at 06:49
  • 1
    Sure. Use `NetUtil.ioService.newChannel(data.url(...), null, null).open()` to get the input stream and `FileUtil.openFileOutputStream(...)` to get the output stream. Then use `NetUtil.asyncCopy()` to copy from one to the other. – Wladimir Palant Mar 07 '12 at 07:23
  • I am not sure that i understand the above comment. what would be the best approach to do the above. should i create an empty sqlite file in the profile directory on addon install(or first run) and get the tables populated with data from a sqlite db hosted somewhere on the internet. Kindly elaborate the comment since i am newbie in addon development. – Pankaj Kumar Mar 07 '12 at 11:15
  • The above is how you would generally copy a file - that might be easier than creating an empty database and populating it. – Wladimir Palant Mar 07 '12 at 11:29