Hi everyone. I hope someone can help me with these errors. I can’t see why my data won’t get put into my tables. I’m completely out of ideas and web searches.
Here is my shell script in case someone can see what I’m doing wrong so far. I have given my script executable permissions and cannot get past this error. Any help would be greatly appreciated.
#! /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 TABLE games, teams")
cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
# exclude the first row of column identifiers
if [[ $YEAR != 'year' ]]
then
# get the winner id
WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
# check if the winner id is empty
if [[ -z $WINNER_ID ]]
then
# if winner id is empty insert winner in the teams table
echo "$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")"
fi
# opponent id
OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
# is the opponent null
if [[ -z $OPPONENT_ID ]]
then
# insert the opponent into the teams table
echo "$($PSQL "INSERT INTO teams(name) VALUES('OPPONENT')")"
# opponent id as a foreign key in the games table
OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
fi
# insert all the data at once into the games table
echo "$($PSQL "INSERT INTO games(year, round, winner, opponent, winner_goals, opponent_goals, winner_id, opponent_id) VALUES($YEAR, '$ROUND', $WINNER, $OPPONENT, $WINNER_GOALS, $OPPONENT_GOALS, $WINNER_ID, $OPPONENT_ID)")"
fi
# winner id for the games table
done