Created
November 4, 2022 06:26
-
-
Save kuc-arc-f/861a4d833adcab3cdce230d470bc9628 to your computer and use it in GitHub Desktop.
SQLite3 WASM/JS download sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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