Connect Addon with sqlite db

SQLite can have problems with databases exposed over the network depending upon how well the file-locking semantics are implemented by the file server/protocol in use. See https://www.sqlite.org/faq.html#q5 for more information.

Assuming it works, using a shared database directly like this can also lead to versioning problems. If you need to upgrade the schema of the database, you have to have some plan in-place for dealing with multiple versions of your add-on, etc. The simplest strategy would just be to switch to a new file/path whenever you issue a breaking schema change.

If you’d like to avoid compatibility and versioning issues, running a simple node.js app on a server on your network could work a lot better.

The gateway to SQLite from Firefox add-ons is the Storage API.

Hey Guys,

hope, you can help me open the sqlite file.

I’ve tried

var file = FileUtils.getFile("V://Tools//data//", ["SITop100.sqlite"]);

to get the File, which is located on one of our Servers. But jpm returns an error and i couldn’t find an example for my case.

Can You show me, how getFile has to look like?

The first parameter to getFile() is not a path, but a “key” that indicates a particular special location. In most cases, use “ProfD” which indicates the profile directory for the currently running browser. Then you specify the filename in the second parameter, which you have done correctly. You can see a list of special location keys here:

If you want to specify an explicit path, you can do so to the FileUtils.File() constructor. Don’t do this because it isn’t platform independent. It is for rare cases where you might have obtained a native path by some means.

Well, since “V://Tools//data//” is not an “nsIDirectoryService key” (see a list of those here), you cannot use it in FileUtils.getFile().
As the documentation of FileUtils states, if you have an absolute path, you should use the File constructor like so:

 var f = new FileUtils.File(mypath);

But that is not even necessary, because Sqlite.jsm expects a path to the database file (an absolute path, or a relative one (that is relative to the current profile directory)), not an nsIFile instance. So you don’t need to instantiate one, just give the path to Sqlite.jsm.

Hey Lithopsian,

thank you for your answer. In my case, the Filepath is fix. This Addon is only for internal use, where I know, that all Users have the same Path to the File.

I will try the FileUtils.File().

@Endyl:

So the right way is:

 var f = new FileUtils.File("V://Tools//data//SITop100.sqlite");

?

To create an nsIFile instance, yes, that is the way. But for Sqlite.jsm you only need something like this:

Sqlite.openConnection({ path: "V://Tools//data//SITop100.sqlite", sharedMemoryCache: false }).then(
  function onConnection(connection) {
    // connection is the opened SQLite connection (see below for API).
  },
  function onError(error) {
    // The connection could not be opened. error is an Error describing what went wrong.
  }
);

Also, don’t forget to import Sqlite.jsm :smile:

Components.utils.import("resource://gre/modules/Sqlite.jsm");
1 Like

OK, thank you very much.

I will try that.

I would still recommend that you don’t specify a platform-dependant absolute path. openConnection will accept a relative path, relative to the profile directory. Use a link if you need to. You’ll be cursing that absolute path one day when v: has a different letter.

@Lithopsian: Can you please give me an example?

if your sqlite file is on the desktop get it like this:

var myfile = Services.dirsvc.get('Desk', Ci.nsIFile);
myfile.append('name-of-file.sqlite');

or with FileUtils, this is like 2ms slower though:

var myfile = FileUtils.getFile('Desk', ['name-of-file.sqlite']);

Here’s a list of special key words: https://gist.github.com/Noitidart/715840fa5008ee032017#file-_ff-addon-snippet-specialdirpaths-js-L29

Also OS.Constants.Path has some special paths, thats my preferred way.

var myfile = OS.Constants.Path.join(OS.Constants.Path.desktopDir, 'name-of-file.sqlite');

Thank you for the list, but i am really new to this topic, so for me it is really hard to understand, how to get my File from the Server.

I think, i understood, how to get Files from special directories like my profile, but how do i get a file from our internal network?

I don’t know, if i schould start a new thread, but another great gap in know how, belonging to this project, is to upload files on a ftp-server. Maybe, you can give me a first hint, where to find an example for file-Upload. If it doesn’t fit here, i will open an new thread for that. I only need a simple upload of defined files to a defined remote directory.

Ah ok if its a path on your network, you have no other choice then to use the hard coded path. if that path changes in the future (due to you guys changing your network) then you should update the addon

I thought hardlinks dont work over the network? I created an addon to create hardlinks on windows: https://addons.mozilla.org/en-US/firefox/addon/deskcuts/

Yeah hard links over network dont work for sure.
Symlink may i dont know i didnt look into that, but that needs admin privileges to create.

Symlinks are the business. Windows supports them now, so join us in the 20th century :wink:

You would rarely want to use a hard link. Despite sounding as if they are better, I’ve yet to come across them in use in the real world. Unless you understand the implications, best to stay away from these.

Symlinks work across networks, or certain networks anyway. I don’t know if you can point one to a V-drive since that is not a persistent name, just a run-time label that could change the next time you boot.

Ah yeah sylinks will probably work, but you do need admin privelages to create them. Or did that change?

Ill hook up deskcuts with symlink support windows too :slight_smile: after they accept ver1

Verified sym link on win needs elevated privileges. Will still land this to deskctus, but heres all the info:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa363878(v=vs.85).aspx