Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 13, 2023 00:42
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 parzibyte/90367845e957bf653c625a3aa6fabe2f to your computer and use it in GitHub Desktop.
Save parzibyte/90367845e957bf653c625a3aa6fabe2f to your computer and use it in GitHub Desktop.
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
console.log("Hola")
const log = (...args) => { console.log(...args) };
const error = (...args) => { console.log(...args) };
const start = function (sqlite3) {
log('Running SQLite3 version', sqlite3.version.libVersion);
let db;
if ('opfs' in sqlite3) {
db = new sqlite3.oo1.OpfsDb('/mydb.sqlite3');
log('OPFS is available, created persisted database at', db.filename);
} else {
db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
log('OPFS is not available, 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);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment