-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.sh
More file actions
107 lines (97 loc) · 3.34 KB
/
insert_data.sh
File metadata and controls
107 lines (97 loc) · 3.34 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /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.
#echo $($PSQL "TRUNCATE games,teams")
echo $($PSQL "TRUNCATE teams,games")
cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
if [[ $YEAR != 'year' ]]
then
#Insert into teams
#First, insert winners
WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
if [[ -z $WINNER_ID ]]
then
#Insert winner into teams
INSERT_WINNER=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
if [[ $INSERT_WINNER == "INSERT 0 1" ]]
then
echo Inserted into teams, $WINNER
fi
fi
#Second, insert losers
LOSER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
if [[ -z $LOSER_ID ]]
then
#Insert loser into teams
INSERT_LOSER=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")
if [[ $INSERT_LOSER == "INSERT 0 1" ]]
then
echo Inserted into teams, $OPPONENT
fi
fi
fi
done
cat games.csv | while IFS=',' read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
if [[ $YEAR != 'year' ]]
then
WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
LOSER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
#Insert into games
INSERT_GAME=$($PSQL "INSERT INTO games(year, round, winner_id, opponent_id, winner_goals, opponent_goals) VALUES($YEAR, '$ROUND', $WINNER_ID, $LOSER_ID, $WINNER_GOALS, $OPPONENT_GOALS)")
if [[ $INSERT_GAME == "INSERT 0 1" ]]
then
echo Inserted into games, $YEAR "$ROUND" $WINNER_ID $LOSER_ID $WINNER_GOALS $OPPONENT_GOALS
fi
fi
done
:<<'ExampleComment'
PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
echo $($PSQL "TRUNCATE students, majors, courses, majors_courses")
cat courses.csv | while IFS="," read MAJOR COURSE
do
if [[ $MAJOR != "major" ]]
then
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
# if not found
if [[ -z $MAJOR_ID ]]
then
# insert major
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
then
echo Inserted into majors, $MAJOR
fi
# get new major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
fi
# get course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
# if not found
if [[ -z $COURSE_ID ]]
then
# insert course
INSERT_COURSE_RESULT=$($PSQL "INSERT INTO courses(course) VALUES('$COURSE')")
if [[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]]
then
echo Inserted into courses, $COURSE
fi
# get new course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
fi
# insert into majors_courses
INSERT_MAJORS_COURSES_RESULT=$($PSQL "INSERT INTO majors_courses(major_id, course_id) VALUES($MAJOR_ID, $COURSE_ID)")
if [[ $INSERT_MAJORS_COURSES_RESULT == "INSERT 0 1" ]]
then
echo Inserted into majors_courses, $MAJOR : $COURSE
fi
fi
done
ExampleComment