Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created November 3, 2022 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/47539060582e97d90b61e030ae34dac2 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/47539060582e97d90b61e030ae34dac2 to your computer and use it in GitHub Desktop.
SQLite3 WASM/JS file load sample
<!DOCTYPE html>
<!-- SQLite3 WASM/JS file load sample -->
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>SQLite</title>
</head>
<body>
<h3>t3.html</h3>
<hr />
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.6.2/sql-wasm.min.js"></script>
<script>
initSqlJs({
locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.6.2/${file}`
}).then(async function(SQL) {
let sqlFilePath = "/sqlite3_wasm/mydb.sqlite";
const dataPromise = await fetch(sqlFilePath).then(res => res.arrayBuffer());
const u8array = new Uint8Array(dataPromise);
const db = new SQL.Database(new Uint8Array(u8array));
let query = "SELECT * FROM users";
let contents = db.exec(query);
res = JSON.stringify(contents);
console.log(contents);
//console.log(contents[0].values[0]);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment