如何初始化Firefox附加组件的SQLite文件? [英] How to initialize SQLite file for Firefox add-on?

查看:126
本文介绍了如何初始化Firefox附加组件的SQLite文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 加载项使用的SQLite数据文件是否可以成为使用data.url()访问的文件之一?

  1. Is it possible for an SQLite data file used by an add-on to be one of the files accessed with data.url()?

如果是这样,如何将其交给Services.storage.openDatabase()

If so, how does one hand it off to Services.storage.openDatabase()

如果没有,是否可能某些代码(CREATE TABLE IF EXISTS ...)仅在首次运行的附加组件中执行?

If not, is it possible for certain code (CREATE TABLE IF EXISTS...) to be executed only in a first-time run of an add-on?


推荐答案


附加组件使用的SQLite数据文件是否可以成为使用data.url()访问的文件之一?

Is it possible for an SQLite data file used by an add-on to be one of the files accessed with data.url()?

不。从Add-on SDK 1.5开始,扩展在安装时不再解压缩 - 它们作为打包的XPI文件保留在磁盘上(这对性能有利)。 SQLite需要一个物理文件,而不是存档中的内容。

No. As of Add-on SDK 1.5, extensions are no longer uncompressed upon installation - they stay as packed XPI files on disk (which is good for performance). SQLite needs a physical file however, not something inside an archive.


如果没有,是否可能某些代码(CREATE TABLE IF EXISTS。 ..)只能在第一次加载时执行?

If not, is it possible for certain code (CREATE TABLE IF EXISTS...) to be executed only in a first-time run of an add-on?

当然但你不应该这样做像这样 - 如果您的数据库文件由于某种原因被删除怎么办?最好检查数据库是否已经存在:

Sure but you shouldn't do it like this - what if your database file gets deleted for some reason? It is better to check whether the database already exists:

var dbFile = FileUtils.getFile("ProfD", "foobar.sqlite");
var alreadyExists = dbFile.exists();
var dbConnection = Services.storage.openDatabase(dbFile);
if (!alreadyExists)
  connection.createTable("foo", "id INTEGER PRIMARY KEY, ...");

供参考: FileUtils.jsm

这篇关于如何初始化Firefox附加组件的SQLite文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆