Tell us what’s happening:
Question, I tried to make a test file (gtest.csv) but it keeps pulling from games.csv. To be honest I’m not sure where exactly the code is pulling from. I made an edit to games.csv and it didn’t print out my change. Is there a reason its doing this?
I’m using the bash command ./insert_data.sh … so I’m not quite sure why it’s pulling from games.csv, when I have switched the to cat gtest.csv
.
Any thoughts would be super helpful!
My Code
#! /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.
# Script to insert data from games.csv into worldcup database
# Clear tables
$PSQL "TRUNCATE TABLE games, teams;"
cat gtest.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
if [[ $WINNER != "winner" ]]
then
#get team id
TEAM_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
#if no team_id
if [[ -z $TEAM_ID ]]
then
#Add new team name
INSERT_TEAMNAME_W=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
if [[ $INSERT_TEAMNAME_W == "INSERT 0 1" ]]
then
echo Inserted new team, $WINNER
fi
#get new team_id
TEAM_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
fi
fi
done
Your browser information: Chrome
Challenge: World Cup Database - Build a World Cup Database
Link to the challenge: