· 7 years ago · Dec 01, 2018, 09:08 AM
1async function csvToSql(data_path, data_type) {
2 const date = moment().format("DD-MM-YYYY")
3 let result = await csv().fromFile(data_path);
4
5 db.serialize(function () {
6 // Create table
7 db.run(`CREATE TABLE IF NOT EXISTS daily(Date DATE, Name VARCHAR(255), Profile TEXT, Strength Int, Speed Int, Dexterity Int, Defense Int)`, (err) => {
8 if (err) {
9 console.log(err);
10 }
11 });
12
13 //Populate
14 let query = db.prepare(`INSERT OR REPLACE INTO daily (Date, Name, Profile, ${data_type} ) VALUES (?, ?, ?, ?)`);
15 if (statType.includes(data_type)) {
16 for (let i = 0, j = result.length; i < j; i++) {
17 query.run(date, result[i].Player, result[i]['Player Profile'], result[i].Contribution, (err) => {
18 if (err) {
19 console.log(err);
20
21 }
22 })
23 }
24
25 }
26 query.finalize();
27 });
28}