-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
40 lines (36 loc) · 1.12 KB
/
Copy pathrun.js
File metadata and controls
40 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(async () => {
// Load sql.js from CDN (or you could also host this JS file on your site)
const initSqlJs = await import('https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.10.2/sql-wasm.js');
const SQL = await initSqlJs.default({
locateFile: file => `https://sap-ai.github.io/${file}` // Host .wasm here
});
// Create a database
const db = new SQL.Database();
db.run(`
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
`);
function post(te, nm) {
db.run("INSERT INTO " + te + "(v) VALUES ('" + nm + "');");
}
// Query the data
const result = [];
function look(obj) {
result = db.exec("SELECT * FROM " + obj);
}
// Log the results
if (result.length > 0) {
const columns = result[0].columns;
const values = result[0].values;
console.log("Users:");
values.forEach(row => {
let user = {};
row.forEach((val, i) => user[columns[i]] = val);
console.log(user);
});
} else {
console.log("No data found.");
}
// Optional: Export the database as binary
const binaryArray = db.export();
console.log("DB saved to memory:", binaryArray);
})();