I’m trying to populate the databases but when I go to insert the data in the insert_data.sh script I get a command not found error on the psql statements.
#! /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.
IFS=“,”
cat $1 | while read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
if [[ $YEAR != “year” ]]
then
RESULT=$($PSQL “INSERT INTO teams(name) VALUES (‘$WINNER’),(‘$OPPONENT’) ON CONFLICT (name) DO NOTHING;
INSERT INTO games(year, round, winner_id, opponent_id, winner_goals, opponent_goals) VALUES($YEAR, ‘$ROUND’, (SELECT team_id FROM teams WHERE name = ‘$WINNER’), (SELECT team_id FROM teams WHERE name = ‘$OPPONENT’), $WINNER_GOALS, $OPPONENT_GOALS);”)
fi
done
I’m pretty new at dev and just started learning in FCC but this is what I’ved tried with your code. I didn’t know you can put IFS=”,” outside the cat | while read I will try it next time but yeah it worked when I just tried doing it my way which how FCC thought me.
team_id | name
---------±--------------
1 | France
2 | Croatia
3 | Belgium
4 | England
10 | Russia
12 | Sweden
14 | Brazil
16 | Uruguay
18 | Colombia
20 | Switzerland
22 | Japan
24 | Mexico
26 | Denmark
28 | Spain
30 | Portugal
32 | Argentina
33 | Germany
35 | Netherlands
42 | Costa Rica
50 | Chile
54 | Nigeria
56 | Algeria
60 | Greece
64 | United States
(24 rows)
There’s the result of the teams table but I had to type out the contents of the PSQL variable to get it to run. I think that is the problem because it fails just the inserting data checks.
I’m fairly certain that my problem lies in not using the PSQL variable to do my insert, but when I use the variable I get the command not found error on the insert. The error shows the psql –username=freecodecamp –dbname=worldcup -t –no-align -c: command not found.
Yeah that’s what showing on my end too when the IFS was declared before the cat $1 | while read too. I don’t know what’s wrong but yeah declaring it inside changes something I don’t know and it might work on your end too. We might have to wait for a more knowledgable dev to explain it for us.
changed it and it worked but I think I have to start over since I populated the tables by hard coding the variable. It runs now but I still fail the tests even after truncating the tables. I checked them and they are fine 24 rows in teams and 32 in games and the queries work.
Yeah that happened to me too. Most of the time, I just click “reset” when I think I already have satisfied the requirements but it’s still not accepting it after checking all the requirements.