closing db to persist

This commit is contained in:
Martin Donnelly 2015-11-09 08:10:52 +00:00
parent 44c9a71095
commit 29a6bd4d9a

View File

@ -14,6 +14,13 @@ function createTable() {
temp_db.run('CREATE TABLE IF NOT EXISTS temp (date text, reading real);');
}
function closeDB() {
"use strict";
console.log('Closing db.');
temp_db.close();
}
function insertTempReading(time, reading) {
console.log('Inserting reading:' + reading);
@ -36,19 +43,27 @@ router.get('/', function(req, res, next) {
{
// createDB();
console.log(req.query);
createDB();
insertTempReading(now.toJSON(),parseFloat(req.query.temp) );
closeDB();
}
res.writeHead(200, {"ContentType": "application/json"});
//res.send(JSON.stringify(t));
res.end(JSON.stringify(t));
});
router.get('/all', function(req, res, next) {
"use strict";
console.log('Selecint all..');
createDB();
temp_db.all('SELECT * from temp', function(err, rows) {
var out = [];
@ -65,6 +80,7 @@ router.get('/all', function(req, res, next) {
//res.send(JSON.stringify(t));
res.end(JSON.stringify(out));
closeDB();
});
@ -73,4 +89,4 @@ router.get('/all', function(req, res, next) {
module.exports = router;
createDB();
//createDB();