forked from TryGhost/node-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple-chaining.js
More file actions
50 lines (39 loc) · 1.01 KB
/
simple-chaining.js
File metadata and controls
50 lines (39 loc) · 1.01 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
/**
* Shows how to use chaining rather than the `serialize` method.
*/
"use strict";
var sqlite3 = require('sqlite3').verbose();
var db;
function createDb() {
console.log("createDb chain");
db = new sqlite3.Database('chain.sqlite3', createTable);
}
function createTable() {
console.log("createTable lorem");
db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)", insertRows);
}
function insertRows() {
console.log("insertRows Ipsum i");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize(readAllRows);
}
function readAllRows() {
console.log("readAllRows lorem");
db.all("SELECT rowid AS id, info FROM lorem", function(err, rows) {
rows.forEach(function (row) {
console.log(row.id + ": " + row.info);
});
closeDb();
});
}
function closeDb() {
console.log("closeDb");
db.close();
}
function runChainExample() {
createDb();
}
runChainExample();