How to use SQLite in Firefox Extension (XUL files) with Javascript source?

643 views Asked by At

I wanted to write a Firefox Extension which creates an SQLite database, then puts some records in it. However, I have an error during running the statement.

Here's the xul file:

<?xml version="1.0"?>
<overlay id="sample" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">
  <script type="application/x-javascript" src="chrome://sqlitetry/content/sqlitetry.js"/>
  <statusbar id="status-bar">
    <statusbarpanel id="my-panel" label="Welcome to SQLite Try 1.0b."  />
  </statusbar>
  <html:div id="status">
  </html:div>
</overlay>

This is the javascript plugin which tries to create the database and the records:

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm")

var dbFile = FileUtils.getFile("ProfD", ["test.sqlite"]);
var dbService = Components.classes["@mozilla.org/storage/service;1"].
getService(Components.interfaces.mozIStorageService);
var dbConnection;
console.log("CONNECT...")
dbConnection = dbService.openDatabase(dbFile);
console.log("\tOK")
var statement = dbConnection.createStatement("SELECT * FROM mytest");
var res = statement.executeStep();

But there is an error in the browser console like this:

NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

Here's the complete source of the extension with the script: http://speedy.sh/4nhsf/source4.xpi

Could someone help please, what the problem is?

1

There are 1 answers

0
Nickolay On

Well, you didn't create the mytest table, so the SELECT statement fails. Try

...
dbConnection = dbService.openDatabase(dbFile);
console.log("\tOK")
dbConnection.createTable("mytest", "foo INTEGER, bar STRING");
dbConnection.executeSimpleSQL("insert into mytest (foo,bar) values (1,'test')")
...