【发布时间】:2011-09-06 06:09:45
【问题描述】:
是否可以制作与 sqlite 数据库交互的 chrome 扩展,类似于 firefox 扩展?你能给我一些建议或链接吗?关于开发与 sqlite 交互的 chrome 扩展的更多信息在哪里?
谢谢
【问题讨论】:
标签: javascript sqlite google-chrome-extension
是否可以制作与 sqlite 数据库交互的 chrome 扩展,类似于 firefox 扩展?你能给我一些建议或链接吗?关于开发与 sqlite 交互的 chrome 扩展的更多信息在哪里?
谢谢
【问题讨论】:
标签: javascript sqlite google-chrome-extension
您可以在浏览器中使用Web SQL API,这是一个普通的 SQLite 数据库,您可以像使用任何其他 SQLite 数据库一样打开/修改它,例如使用 Lita。
Chrome 会根据域名或扩展 ID 自动定位数据库。几个月前,我在how to delete Chrome's database 上发布了我的博客短文,因为当您测试某些功能时,它非常有用。
【讨论】:
您也许可以使用sql.js。
sql.js 是 SQLite 到 JavaScript 的一个端口,通过使用 Emscripten 编译 SQLite C 代码。这里没有 C 绑定或 node-gyp 编译。
<script src='js/sql.js'></script>
<script>
//Create the database
var db = new SQL.Database();
// Run a query without reading the results
db.run("CREATE TABLE test (col1, col2);");
// Insert two rows: (1,111) and (2,222)
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
// Prepare a statement
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
// Bind new values
stmt.bind({$start:1, $end:2});
while(stmt.step()) { //
var row = stmt.getAsObject();
// [...] do something with the row of result
}
</script>
sql.js 是单个 JavaScript 文件,目前大小约为 1.5MiB。虽然这可能是网页中的问题,但扩展的大小可能是可以接受的。
【讨论】:
Chrome 支持 WebDatabase API(由 sqlite 提供支持),但看起来 W3C 停止了它的 development。
【讨论】:
我不太确定你的意思是“我可以在 chrome 中使用 sqlite (websql)”还是“我可以在 firefox 中使用 sqlite (websql)”,所以我会同时回答:
请注意,WebSQL 不是 .sqlite 数据库的完全访问管道。它是 WebSQL。您将无法运行某些特定查询,例如 VACUUM
虽然创建/读取/更新/删除非常棒。 我创建了一个小库,它可以帮助解决所有烦人的细节问题,例如创建表和查询,并提供了一个带有关系的小 ORM/ActiveRecord 模式和所有的大量示例,可以让您立即开始使用,you can check that here
另外,请注意,如果您想构建 FireFox 扩展:Their extension format is about to change. 请确保您要花两次时间。
虽然 WebSQL 规范已被弃用多年,但即使在 2017 年的现在,在可预见的时间内似乎仍不会从 Chrome 中删除。 They are tracking usage statistics and there are still a large number of chrome extensions and websites out there in the real world implementing the spec.
【讨论】: