Worldcup database: script erro

Hi help please:
i tried this but

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

echo $($PSQL "TRUNCATE teams, games")

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

do

# Eliminating Title line

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

# get team_id

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

# if not found

if [[ -z $OPPONENT_ID ]]

then

# insert team

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

if [[ INSERT_OPPONENT_TEAM == "INSERT 0 1" ]]

then

echo Inserted into teams, $OPPONENT

fi

# new winner team_id

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

fi

# get team_id

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

# if not found

if [[ -z $WINNER_ID ]]

then

# insert team

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

if [[ INSERT_WINNER_TEAM == "INSERT 0 1" ]]

then

echo Inserted into teams, $WINNER

fi

# new team_id

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

fi

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

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

then

echo Inserted, $YEAR $ROUND $WINNER $OPPONENT $WINNER_ID $OPPONENT_ID $WINNER_GOALS $OPPONENT_GOALS

fi

fi

done

odeally@d04c40c6043a:~/project$ ./insert_data.sh
TRUNCATE TABLE
./insert_data.sh: line 48: syntax error near unexpected token `('
./insert_data.sh: line 48: `LOG_GAME_RESULT=$($PSQL "INSERT INTO games(year, round, winner, opponent, winner_id, opponent_id, winner_goals, opponent_goals") VALUES($YEAR, '$ROUND', '$WINNER', 'OPPONENT', $WINNER_ID, $OPPONENT_ID, $WINNER_GOALS, $OPPONENT_GOALS)"'
codeally@d04c40c6043a:~/project$ 

how to fix it?
regards,

1 Like

In the above, what is the difference between

the first one with quotes and $
The second one with quotes no $
The third one with dollar no quotes?

Hi
the ones with single quotes are VARCHAR and the other ones without sinle quotes are INT.and sorry I missed to type the $ sign with second one.
regards

Look again? There are three different things?

I see you noticed it. Does that solve the problem?

i changed the $ sign but still

TRUNCATE TABLE
./insert_data.sh: line 48: syntax error near unexpected token `('
./insert_data.sh: line 48: `LOG_GAME_RESULT=$($PSQL "INSERT INTO games(year, round, winner, opponent, winner_id, opponent_id, winner_goals, opponent_goals") VALUES($YEAR, '$ROUND', '$WINNER', '$OPPONENT', $WINNER_ID, $OPPONENT_ID, $WINNER_GOALS, $OPPONENT_GOALS)"'

Try to echo all the values before you insert.
Maybe one of them has a weird value.

Also read this article

It explains how to run in trace mode to try to find the line number that is causing the problem.

echo $YEAR $ROUND $WINNER $OPPONENT $WINNER_GOALS $OPPONENT_GOALS
this one is working


I tried to run without the last query

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

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

then

echo Inserted, $YEAR $ROUND $WINNER $OPPONENT $WINNER_ID $OPPONENT_ID $WINNER_GOALS $OPPONENT_GOALS

fi

thats how how it looked (pic).
regards

Looks like you are inserting duplicate values?

I found three mistakes:
OPOONENT_ID ,

opponent_goals") VALUES ,

WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
this should be
OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")

but still LOG_GAME_RESULT part is not working

Are you getting an error still?

will update…sorry codeally.io refused to connect. for the moment.
hopefully will be resolved sooner.

Hi,
sorry I could check it now that i could connect with codeally.
here is what psql prompt shows:
(24 rows)

(END)
there is END at the end which I dont know how to exit from it as after that I cant run it any further.
and this is the error

/insert_data.sh: line 18: get: command not found
ERROR:  column "winner" of relation "games" does not exist
LINE 1: INSERT INTO games(year, round, winner, opponent, winner_goal...
                                       ^
/insert_data.sh: line 18: get: command not found
ERROR:  column "winner" of relation "games" does not exist
LINE 1: INSERT INTO games(year, round, winner, opponent, winner_id, ...
                                       ^
./insert_data.sh: line 18: get: command not found
ERROR:  column "winner" of relation "games" does not exist
LINE 1: INSERT INTO games(year, round, winner, opponent, winner_id, ...
                                       ^
codeally@466adf0b8792:~/project$

this sounds like the column doesn’t exist

try to go to your psql terminal and run the \l games command to see if the column name is spelled correctly

yes thats right, i made chages and the script works but i am not getting green checks for:

  • When you run your insert_data.sh script, it should add each unique team to the teams table. There should be 24 rows

  • When you run your insert_data.sh script, it should insert a row for each line in the games.csv file (other than the top line of the file). There should be 32 rows. Each row should have every column filled in with the appropriate info. Make sure to add the correct ID’s from the teams table (you cannot hard-code the values)

the code is :

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

echo $($PSQL "TRUNCATE teams, games")

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

do

# Eliminating Title line

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

then

# echo $YEAR $ROUND $WINNER $OPPONENT $WINNER_GOALS $OPPONENT_GOALS

get team_id

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

# if not found

if [[ -z $OPPONENT_ID ]]

then

# insert team

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

if [[ INSERT_OPPONENT_TEAM == "INSERT 0 1" ]]

then

echo Inserted into teams, $OPPONENT

fi

# new opp team_id

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

fi

# get team_id

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

# if not found

if [[ -z $WINNER_ID ]]

then

# insert team

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

if [[ INSERT_WINNER_TEAM == "INSERT 0 1" ]]

then

echo Inserted into teams, $WINNER

fi

# new team_id

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

fi

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

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

then

echo Inserted, $YEAR $ROUND $WINNER $OPPONENT $WINNER_ID $OPPONENT_ID $WINNER_GOALS $OPPONENT_GOALS $WINNER_ID

fi

fi

done

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I am sorry i did not know, next time I will try to make sure

script is working fine but these last three are not passing:

  • When you run your insert_data.sh script, it should add each unique team to the teams table. There should be 24 rows

  • When you run your insert_data.sh script, it should insert a row for each line in the games.csv file (other than the top line of the file). There should be 32 rows. Each row should have every column filled in with the appropriate info. Make sure to add the correct ID’s from the teams table (you cannot hard-code the values)

  • You should correctly complete the queries in the queries.sh file. Fill in each empty echo command to get the output of what is suggested with the command above it. Only use a single line like the first query. The output should match what is in the expected_output.txt file

codeally@466adf0b8792:~/project$ ./queries.sh

Total number of goals in all games from winning teams:
68

Total number of goals in all games from both teams combined:
90

Average number of goals in all games from the winning teams:
2.1250000000000000

Average number of goals in all games from the winning teams rounded to two decimal places:
2.13

Average number of goals in all games from both teams:
2.81250000000000000000

Most goals scored in a single game by one team:
7

Number of games where the winning team scored more than two goals:
6

Winner of the 2018 tournament team name:
France

List of teams who played in the 2014 'Eighth-Final' round:
Argentina
Belgium
Brazil
Colombia
Costa Rica
France
Germany
Netherlands

List of unique winning team names in the whole data set:
Argentina
Belgium
Brazil
Colombia
Costa Rica
Croatia
England
France
Germany
Netherlands
Russia
Sweden
Uruguay

Year and team name of all the champions:
2014|Germany
2018|France

List of teams that start with 'Co':
Colombia
Costa Rica

but I am not passing the test for the last three tasks.

yeah so this part you have to work out the issues one by one.
For eg. for the teams table, try to figure out what went wrong. (count the rows, see if there are any missing teams or duplicate teams etc)