World Cup Database errors

I am getting errors when for the select and insert into lines in the bottom of the code when i try to insert data into the games table. Nearly identical lines of code don’t throw errors when i use them at the begining of the code when i insert data into the teams table. If i type the code into the bash it gives the correct result. Also every time i run the code the index increases and doesn’t start at one even though i am erasing the data in the table every time.

#! /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")

cat games.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 not found

if [[ -z $TEAM_ID ]]

then

#insert team

INSERT_TEAM_RESULT=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")

if [[ $INSERT_TEAM_RESULT = "INSERT 0 1" ]]

then

echo Inserted into teams $WINNER

fi

#get new team_id

TEAM_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")

fi

# get team_id

TEAM_ID=$($PSQL "SELECT team_id from teams WHERE name='$OPPONENT'")

# if not found

if [[ -z $TEAM_ID ]]

then

#insert team

INSERT_TEAM_RESULT=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")

if [[ $INSERT_TEAM_RESULT = "INSERT 0 1" ]]

then

echo Inserted into teams $OPPONENT

fi

#get new team_id

TEAM_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")

fi

fi

done

#new loop

cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS

do

if [[ $YEAR != "year" ]]

then

# get winner_id and opponent_id

WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")

OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")

#insert results

INSERT_GAMES_RESULT=$($PSQL "INSERT INTO games(year, round, winner_id, opponent_id, winner_goals, opponent_goals) VALUES ($YEAR, '$ROUND', $WINNER_ID, $OPPONENT_ID, $WINNER_GOALS, $OPPONENT_GOALS)")

if [[ $INSERT_GAMES_RESULT = "INSERT 0 1" ]]

then

echo Inserted into games $YEAR

fi

fi

done

errors

I just dropped your code and tried the tests using it - doesn’t seem like it threw any errors. I did take the time to format it before running the script but I don’t think that would have caused those syntax errors. Seems like it inserted all teams and games correctly. If you haven’t yet - try opening up a different codeally project, close it, and then reopen the World Cup Database challenge. Might just be a bug in the VM?

1 Like

yeah i have been having problems with the code ally for a few days i don’t know what is wrong. Ive been sitting here for hours trying to figure out what is wrong with the code and i guess it is perfectly fine. i have opened and closed freecode camp and restarted it numerous times i still keep getting errors.

I just relogged into the lesson and didnt use the psql -U postgres < worldcup.sql to rebuild my database and it works. Are you not supposed to use this to rebuild your database everytime you log back in?

Also when i was getting the errors if i used select to view a table i wouldnt be able to get back to the psql prompt no matter what i did hitting enter wouldnt work and i would have to close the console and reopen it and reconnect to the database to get back to the psql prompt.