1

我正在尝试从这里学习如何使用 sql.js。https://sql.js.org/#/

我正在关注第一个 html 示例,但我一直在尝试运行它时遇到错误。

我安装了 sql.js 使用npm install sql.js

我从 sql.js 安装中取出 dist 文件夹并将其放入 index.html 所在的测试文件夹中。

我在那里使用了示例代码并尝试在浏览器中打开它,但我遇到了:sql-wasm.js:167 Fetch API cannot load file:///C:/dist/sql-wasm.wasm. URL scheme "file" is not supported.

代码:

<meta charset="utf8" />
<html>
  <script src='C:\\Users\\Rocko\\Documents\\scripts\\AAOA\\nodetest\\dist\\sql-wasm.js'></script>
  <script>
    config = {
      locateFile: filename => `/dist/${filename}`
    }
    // The `initSqlJs` function is globally provided by all of the main dist files if loaded in the browser.
    // We must specify this locateFile function if we are loading a wasm file from anywhere other than the current html page's folder.
    initSqlJs(config).then(function(SQL){
      //Create the database
      const 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
      const 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()) { //
        const row = stmt.getAsObject();
        console.log('Here is a row: ' + JSON.stringify(row));
      }
    });
  </script>
  <body>
    Output is in Javascript console
  </body>
</html>

图片: 在此处输入图像描述

在此处输入图像描述

我一直在尝试让我的测试 webapp 读取我的 sqlite 文件大约 2 周,并且我一直在尝试遵循人们的建议。这是最新的建议,所以我正在尝试学习这一点,但我什至无法完成基本示例。

任何想法,将不胜感激。

谢谢

4

2 回答 2

2

在 sql.js 文档的同一页面之后,我遇到了同样的错误。

通过使用sql-asm.js而不是 sql-wasm.js 可以避免该错误。正如文档在页面下方所说:“ sql-asm.js Sql.js 的旧 asm.js 版本。更慢且更大。出于兼容性原因提供。

这对我来说比启动 Web 服务器或尝试“将 wasm 编码为 base64”更可取,正如其他一些线程所建议的那样(这超出了我的范围,而只是尝试创建一个链接到 sqlite 文件的简单独立 .html 页面)。

于 2021-09-22T22:05:01.340 回答
0

您正在尝试从本地文件加载 WASM 文件,但这不起作用。

但是你可以很容易地在你的本地机器上启动一个网络服务器(通过使用例如NodeJS),然后从那里运行你的测试代码,所以它使用正确的URL而不是你PC上的本地文件来访问它。

于 2021-08-07T16:49:37.240 回答