sql.js
cdnjsで配信しているので、Scrapboxからも使えそう
だめだった。 connect-src
でひっかかってしまう。
jsawait import("/api/code/takker/sql.js/sample.js");
sample.jsimport { installCDN } from "../scrapbox-install-CDN/script.js";
const version = "1.8.0"
await installCDN(`https://cdnjs.cloudflare.com/ajax/libs/sql.js/${version}/sql-wasm.min.js`, {
id: "sqljs-cdn",
});
// We must specify this locateFile function if we are loading a wasm file from anywhere other than the current html page's folder.
const SQL = await initSqlJs({
locateFile: (file) => file === "sql-wasm.wasm" ?
"https://scrapbox.io/files/6339056660dae0001f816bc1.wasm" : // v1.8.0
`https://cdnjs.cloudflare.com/ajax/libs/sql.js/${version}/${file}`,
});
//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)}`);
}