Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created November 4, 2022 06:26
Embed
What would you like to do?
SQLite3 WASM/JS download sample
<!DOCTYPE html>
<!-- SQLite3 WASM/JS download sample -->
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>SQLite</title>
<meta name="description" content="Zenn" />
</head>
<body>
<h3>t4.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(SQL => {
const db = new SQL.Database();
let res = JSON.stringify(db.exec("SELECT sqlite_version();"));
console.log(res)
res = JSON.stringify(db.exec(`CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT);`));
console.log(res)
res = JSON.stringify(db.exec(`INSERT INTO users(name) VALUES ('hoge123');`));
console.log(res)
res = JSON.stringify(db.exec(`INSERT INTO users(name) VALUES ('fuga123');`));
console.log(res)
res = JSON.stringify(db.exec(`SELECT * FROM users;`));
console.log(res)
//export
const uint8Array = db.export();
//console.log(uint8Array);
const blob = new Blob([uint8Array], {type: 'application/octet-binary'});
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `hoge.sqlite`;
a.click();
a.remove()
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment