繁体   English   中英

如何在带有Javascript源的Firefox扩展(XUL文件)中使用SQLite?

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

我想写一个Firefox扩展,它创建一个SQLite数据库,然后在其中放入一些记录。 但是,我在运行语句时遇到错误。

这是xul文件:

<?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>

这是javascript插件,它试图创建数据库和记录:

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();

但是浏览器控制台中有一个错误,如下所示:

NS_ERROR_FAILURE:组件返回失败代码:0x80004005(NS_ERROR_FAILURE)[mozIStorageConnection.createStatement]

以下是脚本扩展的完整来源: http//speedy.sh/4nhsf/source4.xpi

请问有人帮忙,问题是什么?

好吧,你没有创建mytest表,所以SELECT语句失败了。 尝试

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM