Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poll db 1 queries done #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added database.db
Binary file not shown.
58 changes: 58 additions & 0 deletions delete-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./database.db');

const argv = process.argv.slice(2)
const command = argv[0]
const options = argv.slice(1)

switch (command) {
case "Politicians":
let queryDeletePolitician = `
DELETE FROM Politicianss
WHERE id = ${options[0]};
`
db.run(queryDeletePolitician, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses delete data politician`);
}
})
db.close();
break;

case "Voters":
let queryDeleteVoters = `
DELETE FROM Voters
WHERE id = ${options[0]};
`
db.run(queryDeleteVoters, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses delete data voter`);
}
})
db.close();
break;

case "Votes":
let queryDeleteVotes = `
DELETE FROM Voters
WHERE id = ${options[0]};
`
db.run(queryDeleteVotes, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses delete data votes`);
}
})
db.close();
break;
default: console.log(`silahkan gunakan dengan benar`);

}
64 changes: 64 additions & 0 deletions insert-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./database.db');

const argv = process.argv.slice(2)
const command = argv[0]
const options = argv.slice(1)

switch (command) {
case "Politicians":
let queryInsertPolitician = `
INSERT INTO Politicians
(name,party,location,grade_current)
VALUES
("${options[0]}","${options[1]}","${options[2]}",${options[3]});
`
db.run(queryInsertPolitician, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses input data politician`);
}
})
db.close();
break;

case "Voters":
let queryInsertVoters = `
INSERT INTO Voters
(first_name,last_name,gender,age)
VALUES
("${options[0]}","${options[1]}","${options[2]}",${options[3]});
`
db.run(queryInsertVoters, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses input data voter`);
}
})
db.close();
break;

case "Votes":
let queryInsertVotes = `
INSERT INTO Votes
(voterId,politicianId)
VALUES
(${votesDataSplit[0]},${votesDataSplit[1]});
`
db.run(queryInsertVotes, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses input data votes`);
}
})
db.close();
break;
default: console.log(`silahkan gunakan dengan benar`);

}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "poll-db-1",
"version": "1.0.0",
"description": "",
"main": "seed-data.js",
"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",
"dependencies": {
"sqlite3": "^4.0.4"
}
}
2 changes: 1 addition & 1 deletion politicians.csv
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ 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
Zoe Lofgren,D,HI,12.76643582
93 changes: 93 additions & 0 deletions queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./database.db');

db.serialize(function() {
// 1. Menampilkan nama politician, partai dan grade_current politician tersebut yang berada di partai R
// dan memiliki grade_current range 9 s/d 11
let query1 =
`
SELECT name, party, grade_current FROM Politicians
WHERE party = 'R' AND grade_current > 9 AND grade_current < 11
`
db.all(query1, function(err, rows) {
if (err) {
console.log(err);
}
else {
console.log(rows);
}
})


// 2. Menghitung jumlah vote untuk politician Olympia Snowe
let query2 =
`
SELECT count(voterId) AS totalVote, name FROM Votes
JOIN Politicians ON Votes.politicianId = Politicians.id WHERE Politicians.name = 'Olympia Snowe';
`
db.all(query2, function(err, rows) {
if (err) {
console.log(err);
}
else {
console.log('\n');
console.log(rows);
}
})


// 3. Menghitung jumlah vote untuk politician yang nama-nya mengandung kata "Adam"
let query3 =
`
SELECT name, count(voterId) AS totalVote FROM Politicians JOIN Votes
ON Politicians.id = Votes.politicianId WHERE Politicians.name like '%Adam%' GROUP BY politicianId
`
db.all(query3, function(err, rows) {
if (err) {
console.log(err);
}
else {
console.log('\n');
console.log(rows);
}
})


//4. Menampilkan 3 politician beserta nama partai dan lokasi politician tersebut
// yang memiliki suara terbanyak
let query4 =
`
SELECT count(voterId) AS totalVote, name, party, location FROM Politicians JOIN Votes
ON Politicians.id = Votes.politicianId GROUP BY politicianId ORDER BY totalVote DESC LIMIT 3
`
db.all(query4, function(err, rows) {
if (err) {
console.log(err);
}
else {
console.log('\n');
console.log(rows);
}
})


// 5. Menampilkan siapa saja yang melakukan voting ke politician yang bernama Olympia Snowe
let query5 =
`
SELECT * FROM Voters JOIN Votes ON Voters.id = Votes.voterId
JOIN Politicians ON Votes.politicianId = Politicians.id
WHERE Politicians.name = 'Olympia Snowe'
`
db.all(query5, function(err, rows) {
if (err) {
console.log(err);
}
else {
console.log('\n');
console.log(rows);
}
})

})


30 changes: 30 additions & 0 deletions queries.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1. Menampilkan nama politician, partai dan grade_current politician tersebut yang berada di partai R
dan memiliki grade_current range 9 s/d 11

SELECT name, party, grade_current FROM Politicians
WHERE party = 'R' AND grade_current > 9 AND grade_current < 11


2. Menghitung jumlah vote untuk politician Olympia Snowe

SELECT count(voterId) AS totalVote, name FROM Votes
JOIN Politicians ON Votes.politicianId = Politicians.id WHERE Politicians.name = 'Olympia Snowe';


3. Menghitung jumlah vote untuk politician yang nama-nya mengandung kata "Adam"

SELECT name, count(voterId) AS totalVote FROM Politicians JOIN Votes
ON Politicians.id = Votes.politicianId WHERE Politicians.name like '%Adam%' GROUP BY politicianId


4. Menampilkan 3 politician beserta nama partai dan lokasi politician tersebut yang memiliki suara terbanyak

SELECT count(voterId) AS totalVote, name, party, location FROM Politicians JOIN Votes
ON Politicians.id = Votes.politicianId GROUP BY politicianId ORDER BY totalVote DESC LIMIT 3


5. Menampilkan siapa saja yang melakukan voting ke politician yang bernama Olympia Snowe

SELECT * FROM Voters JOIN Votes ON Voters.id = Votes.voterId
JOIN Politicians ON Votes.politicianId = Politicians.id
WHERE Politicians.name = 'Olympia Snowe'
72 changes: 72 additions & 0 deletions seed-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./database.db');
const fs = require('fs')

db.serialize(function() {
//Insert data
let politicians = fs.readFileSync('./politicians.csv', 'utf8')
let politiciansData = politicians.split('\n').slice(1)

for (let i = 0; i < politiciansData.length; i++) {
let politiciansDataSplit = politiciansData[i].split(',')
let queryInsertPolitician = `
INSERT INTO Politicians
(name,party,location,grade_current)
VALUES
("${politiciansDataSplit[0]}","${politiciansDataSplit[1]}","${politiciansDataSplit[2]}",${politiciansDataSplit[3]});
`
db.run(queryInsertPolitician, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses input data politician`);
}
})
}


let voters = fs.readFileSync('./voters.csv', 'utf8')
let votersData = voters.split('\n').slice(1)

for (let i = 0; i < votersData.length; i++) {
let votersDataSplit = votersData[i].split(',')
let queryInsertVoters = `
INSERT INTO Voters
(first_name,last_name,gender,age)
VALUES
("${votersDataSplit[0]}","${votersDataSplit[1]}","${votersDataSplit[2]}",${votersDataSplit[3]});
`
db.run(queryInsertVoters, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses input data voter`);
}
})
}

let votes = fs.readFileSync('./votes.csv', 'utf8')
let votesData = votes.split('\n').slice(1)

for (let i = 0; i < votesData.length; i++) {
let votesDataSplit = votesData[i].split(',')
let queryInsertVotes = `
INSERT INTO Votes
(voterId,politicianId)
VALUES
(${votesDataSplit[0]},${votesDataSplit[1]});
`
db.run(queryInsertVotes, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`sukses input data votes`);
}
})
}
})

db.close();
Loading