-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddData2Sqlite.js
62 lines (56 loc) · 1.62 KB
/
addData2Sqlite.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// add data to a sqlite databases in js
// by node addData2sqlite.js
const readline = require('readline');
const fs = require('fs');
const sqlite3 = require('sqlite3').verbose();
var res = ['mylog.txt','03.10.2018','m1','Area1','ok','Load']
let db;
// exist database
fs.exists('./event.db', function(exists) {
if (exists) {
// File exist
console.log('open database');
db = new sqlite3.Database('./event.db');
DoMain(db,res);
} else {
// database is not existing
console.log('open database and create table');
db = new sqlite3.Database('./event.db',() => {
db.run('CREATE TABLE logTable(logfile TEXT,logdate TEXT,referto TEXT, area TEXT,status TEXT,action TEXT)',() => {
DoMain(db,res);
});
});
}
});
function DoMain(db,res) {
var logEntry = {
logfile:"-",
logdate:"-",
referto:'-',
area:"-",
status:"-",
action:"-"
};
// map fields to entry
entryDate = '-';
logEntry.logfile = res[0];
logEntry.logdate = res[1];
logEntry.action = res[5];
logEntry.area = res[2];
logEntry.referto = res[3];
logEntry.status = res[4];
doAdd2DB(db,logEntry);
console.log(logEntry);
}
function doAdd2DB(db,entry) {
db.run("INSERT OR IGNORE INTO logTable (logfile, logdate, referto, area, status, action) VALUES (?,?,?,?,?,?)",
[entry["logfile"], entry["logdate"], entry["referto"], entry["area"], entry["status"], entry["action"]], function(err) {
if (err) {
return console.log(err.message);
} else {
db.close();
}
// get the last insert id
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
}