-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.sh
More file actions
40 lines (37 loc) · 1.21 KB
/
Copy pathinsert_data.sh
File metadata and controls
40 lines (37 loc) · 1.21 KB
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
#! /bin/bash
if [[ $1 == "test" ]]
then
PSQL="psql --username=postgres --dbname=worldcuptest -t --no-align -c"
else
PSQL="psql --username=freecodecamp --dbname=worldcup -t --no-align -c"
fi
# Do not change code above this line. Use the PSQL variable above to query your database.
$PSQL "truncate teams,games"
cat games.csv | while IFS="," read year round winner opponent winner_goals opponent_goals
do
if [[ $year != 'year' ]]
then
##
ques="$($PSQL "select count(*) from teams where name = '$winner' ")"
if [[ $ques -lt 1 ]]
then
$PSQL "insert into teams(name) values('$winner')"
fi
##
ques="$($PSQL "select count(*) from teams where name = '$opponent' ")"
if [[ $ques -lt 1 ]]
then
$PSQL "insert into teams(name) values('$opponent')"
fi
##
fi
done
cat games.csv | while IFS="," read year round winner opponent winner_goals opponent_goals
do
if [[ $year != 'year' ]]
then
insert_winner_id="$($PSQL "select team_id from teams where name='$winner'")"
insert_opponent_id="$($PSQL "select team_id from teams where name='$opponent'")"
$PSQL "insert into games(year,round,winner_goals,opponent_goals,winner_id,opponent_id) values($year,'$round',$winner_goals,$opponent_goals,$insert_winner_id,$insert_opponent_id)"
fi
done