mozStorage Extension

Introduction

mozStorage is basically an XPCOM wrapper around SQLite. This makes it easy to use the lightweight database engine from XPCOM applications, such as Firefox, Thunderbird, Mozilla Suite, and Sunbird. The source is available from Mozilla, in the storage directory. The IDL files contain most of the existing documentation. You might want to check out the wiki page.

mozStorage will be included as part of Gecko 1.9, which will be used in Firefox and Thunderbird 3.0; it will not be part of the Gecko 1.8, Firefox 1.5, Firefox 2.0, Thunderbird 1.5, or Thunderbird 2.0 releases. I have built it as an extension so it can be used immediately. It's currently experimental, but developers can start using it now to develop a future version of their extension. By using it, you'll also help test it.

Documentation

Documentation from the IDL files, in a pretty HTML format. Here's a table of the contract IDs and what they implement
ImplementsContractIDCID
mozIStorageConnection @mozilla.org/storage/connection;1 623b8b2e-c9f9-4cc3-b15a-f3c96df2cc1c
mozIStorageDataSet 57826606-3c8a-4243-9f2f-cb3fe6e91148
mozIStorageFunction 898d4189-7012-4ae9-a2af-435491cfa114
mozIStorageService @mozilla.org/storage/service;1 22cff01c-1a5a-4b11-9e27-f8f832226489
mozIStorageStatement 656aa634-36e2-4977-802e-79bce39c1024
mozIStorageStatementWrapper @mozilla.org/storage/statement-wrapper;1 eee6f7c9-5586-4eaf-b35c-dca987c4ffd1
mozIStorageValueArray 44fc1d3b-dc91-4d17-8bc5-2069d8fd3cca

Installation

The following binaries were compiled with the Firefox 1.5.0.0 codebase.
Platform
Windows x86 Install
OS X PPC Install
Linux x86 Install

Example Code

Here's some code that shows how to open and query an SQLite database using JavaScript and XPConnect.

// get the storage service
var store = Components.classes["@mozilla.org/storage/service;1"].
getService(Components.interfaces.mozIStorageService);

// get the profile directory
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsILocalFile);
// This makes file point to PROFILE_DIR/foo.sdb
file.append("foo.sdb");
var db = store.openDatabase(file);

// run some SQL, no return values needed
db.executeSimpleSQL(someSQL);

// SQL that we want data from
var statement = 
Components.classes['@mozilla.org/storage/statement-wrapper;1']
.createInstance(Components.interfaces.mozIStorageStatementWrapper);
statement.initialize(db.createStatement(fancySQL));

// step through the rows:
while(statement.step()) {
 // access a column
 statement.columnName;
} 
    

Credits

The mozStorage code was written by Vlad Vukicevic and is copyrighted by Oracle Corporation. The binaries and documentation on this page were generated by Nikolas Coukouma.