-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-sqlite3.js
36 lines (29 loc) · 1.06 KB
/
hello-sqlite3.js
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
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
// remove database if it exists
var filePath = 'prueba.sqlite3';
fs.unlinkSync(filePath);
// Create database
//var db = new sqlite3.Database(':memory:');
var db = new sqlite3.Database('prueba.sqlite3');
db.on('trace', (m)=> console.log(`Running query: ${m}`));
db.on('profile', (m)=> console.log(`Finished query: ${m}`));
/*
serialize puts the execution mode into serialized. This means that at most
one statement object can execute a query at a time. Other statements
wait in a queue until the previous statements executed.
the parallelized method puts the execution mode into parallelized.
This means that queries scheduled will be run in parallel.
*/
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();