diff --git a/politicians.csv b/politicians.csv new file mode 100644 index 0000000..9cd6217 --- /dev/null +++ b/politicians.csv @@ -0,0 +1,21 @@ +name,party,location,grade_current +Aaron Schock,R,IL,11.5362423 +Adam Kinzinger,R,IL,8.995621754 +Adam Schiff,D,LA,13.0347375 +Adam Smith,D,WA,10.23695519 +Anna Eshoo,D,WA,10.65351543 +Bill Cassidy,R,LA,9.285165569 +Candice Miller,R,WA,11.70865922 +Carolyn Maloney,D,NY,13.03522892 +Dana Rohrabacher,R,NY,10.64634133 +Duncan Hunter,R,IL,13.66509733 +Erik Paulsen,R,LA,11.69134788 +Frank Guinta,R,HI,10.94131093 +Frank LoBiondo,R,HI,13.08452404 +John Kerry,D,LA,11.01029616 +Joseph Lieberman,I,NY,7.95536381 +Michael Bennet,D,IL,7.98091322 +Olympia Snowe,R,WA,13.80125838 +Richard Shelby,R,HI,11.31326157 +Yvette Clarke,D,NY,12.45211362 +Zoe Lofgren,D,HI,12.76643582 diff --git a/poll-db.txt b/poll-db.txt new file mode 100644 index 0000000..e69de29 diff --git a/poll.db b/poll.db new file mode 100644 index 0000000..58b3447 Binary files /dev/null and b/poll.db differ diff --git a/poll.md b/poll.md new file mode 100644 index 0000000..9dd4532 --- /dev/null +++ b/poll.md @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/query.js b/query.js new file mode 100644 index 0000000..9514d38 --- /dev/null +++ b/query.js @@ -0,0 +1,46 @@ +const sqlite3 = require('sqlite3').verbose(); +const db = new sqlite3.Database('./poll.db'); + + +function release_0_1() { + db.serialize(function(){ + db.all("SELECT name,location,grade_current,(SELECT COUNT(*) FROM votes WHERE politicians_id = politicians.politicians_id) AS totalValue FROM politicians WHERE grade_current < 9 ORDER BY grade_current ASC", (err,rows)=>{ + console.log("\nRelease 0 1:"); + console.log(rows); + }); + }); +} + + + +function release_0_2() { + db.serialize(function(){ + db.all(`SELECT (SELECT COUNT(*) FROM votes WHERE politicians_id = politicians.politicians_id) AS totalVote, politicians.name AS politicianName,(first_name||' '||last_name) AS voterName,gender FROM voters + LEFT JOIN votes ON politicians.politicians_id = votes.politicians_id + LEFT JOIN politicians ON voters.voters_id = votes.voters_id WHERE totalVote IN (SELECT (SELECT COUNT(*) FROM votes WHERE politicians_id = politicians.politicians_id) AS totalVote FROM politicians ORDER BY totalVote DESC LIMIT 3) + ORDER BY totalVote DESC`,(err,rows)=>{ + if(err) + console.log(err); + console.log("\nRelease 0 2:"); + console.log(rows); + }) + }) +} + +function release_0_3() { + db.serialize(function() { + db.all("SELECT (SELECT COUNT(*) FROM votes WHERE voters_id = voters.voters_id) AS totalVote,(first_name|| ' '||last_name) AS name,gender,age FROM voters WHERE totalVote > 1 ORDER BY totalVote DESC", + (err, rows) => { + if(err) + console.log(err); + console.log("\nRelease 0 3:"); + console.log(rows); + }); + }); +} + + + +// release_0_1(); +// release_0_2(); +release_0_3(); diff --git a/seed-data.js b/seed-data.js new file mode 100644 index 0000000..0a8e2c9 --- /dev/null +++ b/seed-data.js @@ -0,0 +1,48 @@ +//your code here +const sqlite3 = require('sqlite3').verbose(); +const db = new sqlite3.Database('./poll.db'); + +const fs = require('fs'); +const voters = fs.readFileSync('./voters.csv','utf8').split('\n'); +const votes = fs.readFileSync('./votes.csv', 'utf8').split('\n'); +const politicians = fs.readFileSync('./politicians.csv','utf8').split("\n"); + +write_database(politicians,'politicians'); +write_database(voters,'voters'); +write_database(votes,'votes'); + +db.close(); + + +function write_database(arr,table){ + db.serialize(function(){ + for(let i = 1; i < arr.length-1; i++){ + db.run(`INSERT INTO ${table} VALUES (null,${convert_array_value(arr[i])})`); + } + console.log(`Succeed to insert ${table} data to database`); + }); +} + +function convert_array_value(arr){ + let tempArr = arr.split(','); + for(let i = 0;i