-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdateDb.js
41 lines (33 loc) · 1.31 KB
/
updateDb.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
const sqlite3 = require('sqlite3')
const path = require('path')
const schema = require('./schema')
const fs = require('fs')
const moment = require('moment')
function csvToArray(filepath) {
const file = fs.readFileSync(filepath)
const data = file.toString('utf8')
return data.split('\r\n').filter(row => row != '').map(row=> row.split(','))
}
function main(filepath) {
const db = new sqlite3.Database(path.resolve(__dirname, './compiledSqli.db'));
const rows = csvToArray(filepath);
const filename = filepath.split(path.sep).pop()
db.serialize(function () {
db.run(schema);
db.run(`DELETE FROM daily_bhav where filename='${filename}'`)
const stmt = db.prepare("INSERT INTO daily_bhav VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", );
for (let i = 1; i < rows.length; i++) {
const row = rows[i]
const date = moment(row[2],'DD-MMM-yyyy').format('yyyyMMDD')
row.splice(2,0,date)
row.push(filename)
stmt.run(row);
}
stmt.finalize();
db.each(`SELECT count(1) as RECORDS_INSERTED FROM daily_bhav where filename='${filename}'`, function (err, row) {
console.log(row);
});
});
db.close();
}
main(path.resolve(__dirname,'./data/sec_bhavdata_full_01062021.csv'))