Created
November 4, 2022 06:30
-
-
Save kuc-arc-f/dac1c4888718613196de67da86e57f9d to your computer and use it in GitHub Desktop.
SQLite3 WASM/JS file read sample
This file contains 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 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> |
This file contains 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
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