我正在尝试从这里学习如何使用 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 周,并且我一直在尝试遵循人们的建议。这是最新的建议,所以我正在尝试学习这一点,但我什至无法完成基本示例。
任何想法,将不胜感激。
谢谢