使用 Javascript 在浏览器中直接读取 Sqlite3 的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14764707/
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-10-26 22:31:19  来源:igfitidea点击:

What's the best way to read Sqlite3 directly in Browser using Javascript?

javascriptsqlite

提问by Shreeni

For one of our Insights platform, we plan to generate summary SQLite3 databases in the background and let it be rendered on the browser as charts. Currently, we are intending to a server-side endpoint that will service the data requirement.

对于我们的 Insights 平台之一,我们计划在后台生成汇总 SQLite3 数据库,并让它作为图表呈现在浏览器上。目前,我们打算使用一个服务器端端点来为数据需求提供服务。

We are looking to optimize this further by eliminating the server-side endpoint altogether. We are fine (from a security perspective) to expose the SQLite3 directly on S3 and have a javascript module read and generate the charts.

我们希望通过完全消除服务器端端点来进一步优化这一点。我们可以(从安全角度)直接在 S3 上公开 SQLite3,并让 javascript 模块读取并生成图表。

The SQLite3 files are expected to fairly small - perhaps 4-6 columns and perhaps 10-500 rows of data, and all of them containing one table only. Test runs indicate file sizes of less than 15KB. We don't intend to write or manipulate the SQLite3 on the browser. We don't need to cache it on the browser as a WebSQL or an IndexedDB form, but we are ok with using them if that is what is needed.

SQLite3 文件预计相当小——可能有 4-6 列,可能有 10-500 行数据,并且所有这些文件都只包含一个表。测试运行表明文件大小小于 15KB。我们不打算在浏览器上编写或操作 SQLite3。我们不需要将它作为 WebSQL 或 IndexedDB 表单缓存在浏览器上,但如果需要,我们可以使用它们。

From my web searches, We are unable to find a Javascript library that can read a SQLite3 file and query it for results. If you know of any javascript libraries that can do this, then please let us know.

从我的网络搜索中,我们找不到可以读取 SQLite3 文件并查询结果的 Javascript 库。如果您知道任何可以执行此操作的 javascript 库,请告诉我们。

On the other hand, if you think that we shouldn't be doing this for whatever reason, then please throw them as comments/answers too, because this is something we are trying for the first time and seems a little out-of-the-box, so feedback welcome!

另一方面,如果您认为无论出于何种原因我们都不应该这样做,那么也请把它们作为评论/答案扔掉,因为这是我们第一次尝试的东西,似乎有点不合时宜-box,欢迎反馈!

采纳答案by Marcel

I can not tell the best, but one: Write a JavaScript SQLite reader library yourself.This will be a tedious task, but I am sure it can be done. Some cool folks have done pdf.js, which is a JavaScript renderer for PDF files, which are also binary BLOB's like SQLite filesare.

我不能说最好的,但有一个:自己编写一个 JavaScript SQLite 阅读器库。这将是一项乏味的任务,但我相信它可以完成。一些很酷的人已经完成了pdf.js,这是一个用于 PDF 文件的 JavaScript 渲染器,它也是二进制 BLOB,就像SQLite 文件一样。

You will most probably start with the FileReader APIto walk thru the SQLite file, then create some in-memory representation of the content, which your chart tool can use.

您很可能会从FileReader API开始遍历 SQLite 文件,然后创建内容的一些内存表示,供您的图表工具使用。

Disclaimer: You probably want to solve your initial problem with another solution, as proposed by others, but this answers your question.

免责声明:您可能想用其他人提出的另一种解决方案来解决您最初的问题,但这可以回答您的问题。

回答by lovasoa

There is a javascript library called sql.jsthat can do exactly what you want. In your case, you would use it like that

有一个名为 javascript 的库sql.js,可以完全满足您的需求。在你的情况下,你会像那样使用它

const SQL = await initSqlJs(options);
const fetched = await fetch("/path/to/database.sqlite");
const buf = await fetched.arrayBuffer();
const db = new SQL.Database(new Uint8Array(buf));
const contents = db.exec("SELECT * FROM my_table");
// contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]

See the documentation on sql-js.github.io/sql.js/documentation/

请参阅sql-js.github.io/sql.js/documentation/上的文档