【问题标题】:How to use SQLite in Firefox Extension (XUL files) with Javascript source?如何在带有 Javascript 源的 Firefox 扩展(XUL 文件)中使用 SQLite?
【发布时间】:2014-03-18 12:49:12
【问题描述】:

我想编写一个 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

有人可以帮忙吗,问题是什么?

【问题讨论】:

  • 您可以在 Firefox 中使用 SQLite Manager Add on 。它有很多不错的功能。

标签: javascript sqlite firefox


【解决方案1】:

好吧,您没有创建 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')")
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多