From 95ef2774b3c232cb956319f18ec6168ef3eda376 Mon Sep 17 00:00:00 2001 From: Aditya Respati Date: Mon, 10 Dec 2018 20:06:00 +0700 Subject: [PATCH] commit --- database.db | Bin 0 -> 28672 bytes package.json | 23 ++++++ seed-data.js | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++ setup.js | 56 +++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 database.db create mode 100644 package.json diff --git a/database.db b/database.db new file mode 100644 index 0000000000000000000000000000000000000000..98f4b063aa5cd9d2722c2737c8210a96c2222750 GIT binary patch literal 28672 zcmeHPd5|2{eV*%0@6Pn0J!a?HV^*tcb$}2O5)u-7pR=pg>`J>UK;rF8?@nubx>r3j zD@NjKz!-(t<}!{*DzQQiQ%Qx8l!KFC%8nf>B90BXk`Pzy7-K@>;5Y@9!vs_LUccGZ z$_7_C|2gTYLf_l|`uE=NeeZs+ACpJYT1idKn^u9AQrCLcdAwdvS1RT4cn^C#9s&QY z{QL+6R{j8b*MvVLh&Zm=aJK|DAf5CmG5H1QZxL{Rt^%$Ct^%$Ct^%$Ct^%$Ct^%$C zt^)t}6j%-j(P-3bkCy11t{yW>YSHoH!HLF-UY0!e2n(CkVfERT7)A$n{`6mw{U0N@FxR)kveQ<* zLbRj9dvBc+=ftYw#Wg|S6>+U0uGI5q*YZOYT%uN~@&V%LW|o#T)A#^t!J;|!WVUQs zs!{q$^*Y(wyivN(qr9ekLAj{ht(;H}C`tJ}`J3`{@~_Fa%QwjfE22J%B^QOtjjpG*{l^?2Wt`B-TRxPu6ZGOHl-QBgd?ovzR>z{9~De#Fy(@*9ASBVLQ95mT&^xt5a)H?Hix%+VbW(^VxU)s!a)c zI>BeOY@S-V)MQ@O=PDUQu<6s;SH#=@v{mL29M&s^C5@&g4fBkefpgoQICTE&cf8dm z@d(CP1gf6uQ4K>a^$n-H+TJSN`|Z+;?PAcg11XG{MRh5kN^9zzieqSF7*4dkGV=QS z-@iW@=D8m+^G0e^wX6!u-zL(3{wr^Pr!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&gqtZ@K6H?)g94t8&l(u}|lo|GVe^*ptPkuzUXRp8u;|?)kra{?7(S_x#^ES8~t) z*?nO5{NFwQchCQ;A56LD|7?8tIXVA#zFI!W*8hSs#Mb|pl&6))lslC<{OSH&1zZJO z1zZJO1zZJO1zZJO1zZJO1zZJO1zZI_K!K=@8y4&ygL7eTsDe9y{M-FR9N)4y7fbkk z>9yCB8yalfvuMCo0bC&O#jOO&_774q8&@oxtK6oh)7~;}ofr$wMbX_+o87R$)sN$h uO5;`r{%?WIXeesqhJ|y<+tk#xS1%$78&@pYjg9J^+Kje@s}$V3vHt-_Ir`E7 literal 0 HcmV?d00001 diff --git a/package.json b/package.json new file mode 100644 index 0000000..3f74070 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "poll-db-1", + "version": "1.0.0", + "description": "", + "main": "seed-data.js", + "dependencies": { + "sqlite3": "^4.0.4" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/zen-fox-2018/poll-db-1.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/zen-fox-2018/poll-db-1/issues" + }, + "homepage": "https://github.com/zen-fox-2018/poll-db-1#readme" +} diff --git a/seed-data.js b/seed-data.js index e69de29..d877123 100755 --- a/seed-data.js +++ b/seed-data.js @@ -0,0 +1,219 @@ +const fs = require("fs") +const sqlite3 = require('sqlite3').verbose() +const db = require('./setup.js') + +function readFile(path, cb) { + fs.readFile(path, 'utf8', function (err, data) { + if (err) { + cb(err, null) + } else { + cb(null, data) + } + }) +} + +function parsePoliticians() { + db.serialize(function () { + readFile('./politicians.csv', function (err, data) { + if (err) { + console.log(err, 'error') + } else { + let parsedData = data.split("\n").slice(1) + + for (let i = 0; i < parsedData.length; i++) { + let splitData = parsedData[i].split(',') + let insertData = + `INSERT INTO Politicians (name,party,location,grade_current) + VALUES('${splitData[0]}','${splitData[1]}', '${splitData[2]}', ${splitData[3]});` + + db.run(insertData, function (err) { + if (err) { + console.log(err) + } else { + console.log("success add data") + } + }) + } + } + }) + }) +} + +function parseVoters() { + db.serialize(function () { + readFile('./voters.csv', function (err, data) { + if (err) { + console.log(err, 'error') + } else { + let parsedData = data.split("\n").slice(1) + + for (let i = 0; i < parsedData.length; i++) { + let splitData = parsedData[i].split(',') + let insertData = + `INSERT INTO voters (first_name, last_name, gender, age) + VALUES('${splitData[0]}','${splitData[1]}', '${splitData[2]}', ${splitData[3]});` + + db.run(insertData, function (err) { + if (err) { + console.log(err) + } else { + console.log("success fetching data") + } + }) + } + } + }) + }) +} + +function parseVotes() { + db.serialize(function () { + readFile('./votes.csv', function (err, data) { + if (err) { + console.log(err, 'error') + } else { + let parsedData = data.split("\n").slice(1) + + for (let i = 0; i < parsedData.length; i++) { + let splitData = parsedData[i].split(',') + let insertData = + `INSERT INTO votes (voterID, politicianID) + VALUES(${splitData[0]},${splitData[1]});` + + db.run(insertData, function (err) { + if (err) { + console.log(err) + } else { + console.log("success fetching data") + } + }) + } + } + }) + }) +} + +function update(table_name, column_name, value, id){ + let update = + `UPDATE ${table_name} SET ${column_name} = '${value}' WHERE ${table_name}.id = ${id};` + + db.run(update, function(err){ + if(err){ + console.log(err, 'error') + } else{ + console.log('update successful') + } + }) +} + +function deletion(table_name, column_name, value){ + let deletion = + `DELETE FROM ${table_name} WHERE ${column_name} = ${value}` + + db.run(update, function(err){ + if(err){ + console.log(err, 'error') + } else{ + console.log('delete successful') + } + }) + +} + +function showRepublicansByGrade(){ + let query = `SELECT name, party, grade_current FROM Politicians + WHERE party = 'R' AND grade_current BETWEEN 9 AND 11` + + db.all(query, function(err, rows){ + if(err){ + console.log(err, 'error') + } else{ + console.log(rows) + } + }) +} + +function showOlympiaSnoweVote(){ + let query = + `SELECT COUNT (Votes.voterID) AS TotalVote, name + FROM Politicians + INNER JOIN Votes ON Votes.politicianID = Politicians.id + WHERE politicians.name = 'Olympia Snowe'` + + db.all(query, function(err, rows){ + if(err){ + console.log(err) + } else{ + console.log(rows) + } + }) +} + +function showAdam(){ + let query = + `SELECT COUNT (Votes.voterID) AS TotalVote, name + FROM Politicians + INNER JOIN Votes ON Votes.politicianID = Politicians.id + WHERE Politicians.name LIKE '%Adam%' GROUP BY Politicians.name` + + db.all(query, function(err, rows){ + if(err){ + console.log(err) + } else{ + console.log(rows) + } + }) +} + +function HighestVotes(){ + let query = + `SELECT COUNT (Votes.voterID) AS TotalVote, name, party, location + FROM Politicians + INNER JOIN Votes ON Votes.politicianID = Politicians.id + GROUP BY Politicians.name + ORDER BY TotalVote DESC LIMIT 3` + + db.all(query, function(err, rows){ + if(err){ + console.log(err) + } else{ + console.log(rows) + } + }) + +} + +function showVoters(){ + let query = ` + SELECT + first_name, + last_name, + gender, + age + FROM Voters + INNER JOIN Votes ON Votes.voterID = Voters.id + INNER JOIN Politicians ON Votes.politicianID = Politicians.id + WHERE Politicians.name = 'Olympia Snowe' ` + + db.all(query, function(err, rows){ + if(err){ + console.log(err) + } else{ + console.log(rows) + } + }) +} + + +// update('Politicians', 'party', 'D', 1) +// parsePoliticians() +// parseVoters() +// parseVotes() + +// showRepublicansByGrade() +// showOlympiaSnoweVote() +// showAdam() +// HighestVotes() +// showVoters() + + diff --git a/setup.js b/setup.js index bd00424..a51f1f5 100755 --- a/setup.js +++ b/setup.js @@ -1 +1,57 @@ //your code here +const sqlite3 = require('sqlite3').verbose() +const db = new sqlite3.Database('database.db') + +db.serialize(function (err, data) { + if (err) { + console.log(err) + } else { + + let createPoliticians = + `CREATE TABLE Politicians( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT, + party TEXT, + location TEXT, + grade_current REAL + )` + + // db.run(createPoliticians, function (err) { + // if (err) { + // console.log(err) + // } + // }) + + let createVoters = + `CREATE TABLE Voters( + id INTEGER PRIMARY KEY AUTOINCREMENT, + first_name TEXT, + last_name TEXT, + gender TEXT, + age REAL + )` + + // db.run(createVoters, function (err) { + // if (err) { + // console.log(err) + // } + // }) + + let createVotes = + `CREATE TABLE Votes( + voterID INTEGER, + politicianID INTEGER + )` + + // db.run(createVotes, function (err) { + // if (err) { + // console.log(err) + // } + // }) + + } +}) + +// db.close() + +module.exports = db \ No newline at end of file