Javascript 铬中的Sqlite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6200976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:43:16  来源:igfitidea点击:

Sqlite in chrome

javascriptsqlitegoogle-chrome-extension

提问by xralf

Is it possible to make chrome extension that interacts with sqlite database similarly as firefox extension? Could you give me some advise or link where is more info about developing chrome extension interacting with sqlite?

是否可以制作与 Firefox 扩展类似的与 sqlite 数据库交互的 chrome 扩展?你能给我一些建议或链接哪里有关于开发与 sqlite 交互的 chrome 扩展的更多信息吗?

thank you

谢谢你

回答by martin

You can use Web SQL APIwhich is an ordinary SQLite database in your browser and you can open/modify it like any other SQLite databases for example with Lita.

您可以在浏览器中使用Web SQL API,它是一个普通的 SQLite 数据库,您可以像打开/修改任何其他 SQLite 数据库一样打开/修改它,例如使用Lita

Chrome locates databases automatically according to domain names or extension id. A few months ago I posted on my blog short article on how to delete Chrome's databasebecause when you're testing some functionality it's quite useful.

Chrome 会根据域名或扩展程序 id 自动定位数据库。几个月前,我在我的博客上发表了一篇关于如何删除 Chrome 数据库的短文,因为当您测试某些功能时,它非常有用。

回答by sampathsris

You might be able to make use of sql.js.

您也许可以使用sql.js

sql.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten. no C bindings or node-gyp compilation here.

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.jsis a single JavaScript file and is about 1.5MiB in size currently. While this could be a problem in a web-page, the size is probably acceptable for an extension.

sql.js是单个 JavaScript 文件,目前大小约为 1.5MiB。虽然这可能是网页中的一个问题,但扩展程序的大小可能是可以接受的。

回答by serg

Chrome supports WebDatabase API(which is powered by sqlite), but looks like W3C stopped its development.

Chrome 支持WebDatabase API(由 sqlite 提供支持),但看起来 W3C 停止了它的开发

回答by SchizoDuckie

I'm not quite sure if you mean 'can i use sqlite (websql) in chrome' or 'can i use sqlite (websql) in firefox', so I'll answer both:

我不太确定您的意思是“我可以在 chrome 中使用 sqlite (websql)”还是“我可以在 firefox 中使用 sqlite (websql)”,所以我会同时回答:

Note that WebSQL is not a full-access pipe into an .sqlite database. It's WebSQL. You will not be able to run some specific queries like VACUUM

请注意,WebSQL 不是进入 .sqlite 数据库的完全访问管道。这是WebSQL。您将无法运行某些特定查询,例如VACUUM

It's awesome for Create / Read / Update / Delete though. I made a little library that helps with all the annoying nitty gritty like creating tables and querying and a provides a little ORM/ActiveRecord pattern with relations and all and a huge stack of examples that should get you started in no-time, you can check that here

不过,它对于创建/读取/更新/删除来说非常棒。我制作了一个小库,可以帮助解决所有烦人的细节问题,例如创建表和查询,并提供了一个带有关系的小 ORM/ActiveRecord 模式以及所有应该让您立即开始的大量示例,您可以查看这里

Also, be aware that if you want to build a FireFox extension: Their extension format is about to change.Make sure you want to invest the time twice.

另外,请注意,如果您想构建 FireFox 扩展:它们的扩展格式即将更改。确保你想把时间投入两次。

While the WebSQL spec has been deprecated for years, even now in 2017 still does not look like it will be be removed from Chrome for the foreseeable time. 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.

尽管 WebSQL 规范已被弃用多年,但即使在 2017 年的现在看来,在可预见的时间内它仍不会从 Chrome 中删除。他们正在跟踪使用统计数据,并且在现实世界中仍然有大量的 chrome 扩展和网站实施规范