Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created November 4, 2022 06:30
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/dac1c4888718613196de67da86e57f9d to your computer and use it in GitHub Desktop.
Save kuc-arc-f/dac1c4888718613196de67da86e57f9d to your computer and use it in GitHub Desktop.
SQLite3 WASM/JS file read sample
<!DOCTYPE html>
<!-- SQLite3 WASM/JS file read sample -->
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>SQLite</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.6.2/sql-wasm.min.js"></script>
</head>
<body>
<h3>test5_up.html</h3>
<hr />
<div><input type="file" id="file1" class="btn btn-outline-primary">
</div>
<script src="upload.js"></script>
<script>
upload.initUpload();
</script>
</body>
</html>
const upload = {
/**
* initUpload
* @param
*
* @return
*/
initUpload: function(){
const dbFileElm = document.getElementById('file1');
initSqlJs({
locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.6.2/${file}`
}).then(SQL => {
dbFileElm.onchange = () => {
const f = dbFileElm.files[0];
const r = new FileReader();
r.onload = function() {
const Uints = new Uint8Array(r.result);
const db = new SQL.Database(Uints);
let res = JSON.stringify(db.exec("SELECT sqlite_version();"));
console.log(res);
res = JSON.stringify(db.exec(`SELECT * FROM users;`));
console.log(res)
}
r.readAsArrayBuffer(f);
}
});
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment