-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.js
More file actions
34 lines (31 loc) · 1.06 KB
/
Copy pathdatabase.js
File metadata and controls
34 lines (31 loc) · 1.06 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
/* eslint-disable no-console */
const sqlite3 = require('sqlite3').verbose();
const DBSOURCE = 'db.sqlite';
const db = new sqlite3.Database(DBSOURCE, (errConnect) => {
if (errConnect) {
// Cannot open database
console.error(errConnect.message);
throw errConnect;
} else {
console.log('Connected to the SQLite database.');
db.run(
`CREATE TABLE todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
contents text,
done tinyint DEFAULT 0
)`,
(errQuery) => {
if (errQuery) {
// Table already created
} else {
// Table just created, creating some rows
const insert = 'INSERT INTO todo (contents, done) VALUES (?,?)';
db.run(insert, ['Acheter des bières', 0]);
db.run(insert, ['Tondre le jardin', 0]);
db.run(insert, ['Nettoyer le barbecue', 1]);
}
},
);
}
});
module.exports = db;