-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
36 lines (30 loc) · 969 Bytes
/
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
//Create Sqlite database
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('weather.db');
//Insert data into the table getting the values from the sensor
var insert = function(temp, hum) {
var db = new sqlite3.Database('weather.db');
db.serialize(function() {
db.run("INSERT INTO weather VALUES (datetime('now','localtime'), ?, ?)", temp, hum);
})
db.close();
};
//Create a function to read the last row of the table weather
var read = function() {
var db = new sqlite3.Database('weather.db');
return new Promise(function(resolve, reject) {
db.serialize(function() {
db.get("SELECT * FROM weather ORDER BY date DESC LIMIT 1", function(err, row) {
if (err) {
reject(err);
} else {
resolve(row);
}
});
});
});
};
//Export the functions to be used in the other files
module.exports.insert = insert;
module.exports.read = read;
module.exports.db = db;