-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathmain-thread.js
More file actions
53 lines (47 loc) · 1.34 KB
/
main-thread.js
File metadata and controls
53 lines (47 loc) · 1.34 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sqlite3InitModule from '../src/index.js';
const container = document.querySelector('.main-thread');
const logHtml = function (cssClass, ...args) {
const div = document.createElement('div');
if (cssClass) div.classList.add(cssClass);
div.append(document.createTextNode(args.join(' ')));
container.append(div);
};
const log = (...args) => logHtml('', ...args);
const error = (...args) => logHtml('error', ...args);
const start = function (sqlite3) {
log('Running SQLite3 version', sqlite3.version.libVersion);
const db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
log('Created transient database', db.filename);
try {
log('Creating a table...');
db.exec('CREATE TABLE IF NOT EXISTS t(a,b)');
log('Insert some data using exec()...');
for (let i = 20; i <= 25; ++i) {
db.exec({
sql: 'INSERT INTO t(a,b) VALUES (?,?)',
bind: [i, i * 2],
});
}
log('Query data with exec()...');
db.exec({
sql: 'SELECT a FROM t ORDER BY a LIMIT 3',
callback: (row) => {
log(row);
},
});
} finally {
db.close();
}
};
log('Loading and initializing SQLite3 module...');
sqlite3InitModule({
print: log,
printErr: error,
}).then((sqlite3) => {
log('Done initializing. Running demo...');
try {
start(sqlite3);
} catch (err) {
error(err.name, err.message);
}
});