This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
39 lines (36 loc) · 1.6 KB
/
database.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
37
38
39
const DB_NAME = './history.db';
var sqlite = require('sqlite3').verbose();
function insertData(country, dead, recovered, infected, date) {
let db = new sqlite.Database(DB_NAME);
db.run('CREATE TABLE IF NOT EXISTS covid(_id INTEGER NOT NULL, country VARCHAR(125) NOT NULL, dead INTEGER, recovered INTEGER, infected INTEGER, date TIMESTAMP NOT NULL, PRIMARY KEY(_id));', (err) => {
if(err) console.log(err.message);
db.run('INSERT INTO covid (country,dead,recovered,infected,date) VALUES (?,?,?,?,?);', [country, dead, recovered, infected, date], (err) => {
if(err) console.log(err.message);
db.close();
});
});
}
function queryData(country, lastDate) {
return new Promise((success, reject) => {
let db = new sqlite.Database(DB_NAME);
let retVal = [];
db.all('SELECT country,dead,recovered,infected,date FROM covid WHERE country == ? AND date >= ? ORDER BY date DESC;',
[country, lastDate], (err, rows) => {
if(err) reject(err.message);
else {
for(let i = 0; i < rows.length; i++) {
retVal.push({
country: rows[i].country,
dead: rows[i].dead,
recovered: rows[i].recovered,
confirmed: rows[i].infected,
date: rows[i].date
});
}
success(retVal);
}
});
});
}
module.exports.insertData = insertData;
module.exports.queryData = queryData;