World Cup Database - Build a World Cup Database

Tell us what’s happening:
For some reason, my code insert_data.sh is not passing this test, even though on checking it seems to be creating the appropriate table entries.

" 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)"

Your code so far

#! /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") # reset tables to blank state

cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
# Note that this script reads in row-by-row
do
## Bringing in teams table
if [[ $WINNER != 'winner' ]] # removes header from csv
then
    # Start by getting winning team name
  WINNING_TEAM_NAME=$($PSQL "SELECT name FROM teams WHERE name='$WINNER'")
        # if team not in table
      if [[ -z $WINNING_TEAM_NAME ]]
      then
          # insert team
          echo INSERT_WINNING_NAME=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
          # if successfully inserted
          if [[ $INSERT_WINNING_NAME == "INSERT 0 1" ]]
          then
              echo Inserted $WINNER
          fi
      fi      
fi
if [[ $OPPONENT != 'opponent' ]]    # removes header from csv
then
    # Start by getting winning team name
  LOSING_TEAM_NAME=$($PSQL "SELECT name FROM teams WHERE name='$OPPONENT'")
        # if team not in table
      if [[ -z $LOSING_TEAM_NAME ]]
      then
          # insert team
          echo INSERT_LOSING_NAME=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")
          # if successfully inserted
          if [[ $INSERT_LOSING_NAME == "INSERT 0 1" ]]
          then
              echo Inserted $OPPONENT
          fi
      fi      
fi
## Bringing in games table
if [[ YEAR != "year" ]] # removes header from csv
then
    # Get Winner
    WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
    # Get Loser
    LOSER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
    # Insert Game
    echo INSERT_GAME=$($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)")
fi
done

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0

Challenge: World Cup Database - Build a World Cup Database

Link to the challenge:

Could you share dump of your db as well?

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

DROP DATABASE worldcup;
--
-- Name: worldcup; Type: DATABASE; Schema: -; Owner: freecodecamp
--

CREATE DATABASE worldcup WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE worldcup OWNER TO freecodecamp;

\connect worldcup

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: games; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.games (
    game_id integer NOT NULL,
    year integer NOT NULL,
    round character varying NOT NULL,
    winner_goals integer NOT NULL,
    opponent_goals integer NOT NULL,
    winner_id integer NOT NULL,
    opponent_id integer NOT NULL
);


ALTER TABLE public.games OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.games_game_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;


--
-- Name: teams; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.teams (
    team_id integer NOT NULL,
    name character varying NOT NULL
);


ALTER TABLE public.teams OWNER TO freecodecamp;

--
-- Name: teams_team_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.teams_team_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.teams_team_id_seq OWNER TO freecodecamp;

--
-- Name: teams_team_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.teams_team_id_seq OWNED BY public.teams.team_id;


--
-- Name: games game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass);


--
-- Name: teams team_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.teams ALTER COLUMN team_id SET DEFAULT nextval('public.teams_team_id_seq'::regclass);


--
-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.games VALUES (129, 2018, 'Final', 4, 2, 160, 161);
INSERT INTO public.games VALUES (130, 2018, 'Third Place', 2, 0, 162, 163);
INSERT INTO public.games VALUES (131, 2018, 'Semi-Final', 2, 1, 161, 163);
INSERT INTO public.games VALUES (132, 2018, 'Semi-Final', 1, 0, 160, 162);
INSERT INTO public.games VALUES (133, 2018, 'Quarter-Final', 3, 2, 161, 164);
INSERT INTO public.games VALUES (134, 2018, 'Quarter-Final', 2, 0, 163, 165);
INSERT INTO public.games VALUES (135, 2018, 'Quarter-Final', 2, 1, 162, 166);
INSERT INTO public.games VALUES (136, 2018, 'Quarter-Final', 2, 0, 160, 167);
INSERT INTO public.games VALUES (137, 2018, 'Eighth-Final', 2, 1, 163, 168);
INSERT INTO public.games VALUES (138, 2018, 'Eighth-Final', 1, 0, 165, 169);
INSERT INTO public.games VALUES (139, 2018, 'Eighth-Final', 3, 2, 162, 170);
INSERT INTO public.games VALUES (140, 2018, 'Eighth-Final', 2, 0, 166, 171);
INSERT INTO public.games VALUES (141, 2018, 'Eighth-Final', 2, 1, 161, 172);
INSERT INTO public.games VALUES (142, 2018, 'Eighth-Final', 2, 1, 164, 173);
INSERT INTO public.games VALUES (143, 2018, 'Eighth-Final', 2, 1, 167, 174);
INSERT INTO public.games VALUES (144, 2018, 'Eighth-Final', 4, 3, 160, 175);
INSERT INTO public.games VALUES (145, 2014, 'Final', 1, 0, 176, 175);
INSERT INTO public.games VALUES (146, 2014, 'Third Place', 3, 0, 177, 166);
INSERT INTO public.games VALUES (147, 2014, 'Semi-Final', 1, 0, 175, 177);
INSERT INTO public.games VALUES (148, 2014, 'Semi-Final', 7, 1, 176, 166);
INSERT INTO public.games VALUES (149, 2014, 'Quarter-Final', 1, 0, 177, 178);
INSERT INTO public.games VALUES (150, 2014, 'Quarter-Final', 1, 0, 175, 162);
INSERT INTO public.games VALUES (151, 2014, 'Quarter-Final', 2, 1, 166, 168);
INSERT INTO public.games VALUES (152, 2014, 'Quarter-Final', 1, 0, 176, 160);
INSERT INTO public.games VALUES (153, 2014, 'Eighth-Final', 2, 1, 166, 179);
INSERT INTO public.games VALUES (154, 2014, 'Eighth-Final', 2, 0, 168, 167);
INSERT INTO public.games VALUES (155, 2014, 'Eighth-Final', 2, 0, 160, 180);
INSERT INTO public.games VALUES (156, 2014, 'Eighth-Final', 2, 1, 176, 181);
INSERT INTO public.games VALUES (157, 2014, 'Eighth-Final', 2, 1, 177, 171);
INSERT INTO public.games VALUES (158, 2014, 'Eighth-Final', 2, 1, 178, 182);
INSERT INTO public.games VALUES (159, 2014, 'Eighth-Final', 1, 0, 175, 169);
INSERT INTO public.games VALUES (160, 2014, 'Eighth-Final', 2, 1, 162, 183);


--
-- Data for Name: teams; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.teams VALUES (160, 'France');
INSERT INTO public.teams VALUES (161, 'Croatia');
INSERT INTO public.teams VALUES (162, 'Belgium');
INSERT INTO public.teams VALUES (163, 'England');
INSERT INTO public.teams VALUES (164, 'Russia');
INSERT INTO public.teams VALUES (165, 'Sweden');
INSERT INTO public.teams VALUES (166, 'Brazil');
INSERT INTO public.teams VALUES (167, 'Uruguay');
INSERT INTO public.teams VALUES (168, 'Colombia');
INSERT INTO public.teams VALUES (169, 'Switzerland');
INSERT INTO public.teams VALUES (170, 'Japan');
INSERT INTO public.teams VALUES (171, 'Mexico');
INSERT INTO public.teams VALUES (172, 'Denmark');
INSERT INTO public.teams VALUES (173, 'Spain');
INSERT INTO public.teams VALUES (174, 'Portugal');
INSERT INTO public.teams VALUES (175, 'Argentina');
INSERT INTO public.teams VALUES (176, 'Germany');
INSERT INTO public.teams VALUES (177, 'Netherlands');
INSERT INTO public.teams VALUES (178, 'Costa Rica');
INSERT INTO public.teams VALUES (179, 'Chile');
INSERT INTO public.teams VALUES (180, 'Nigeria');
INSERT INTO public.teams VALUES (181, 'Algeria');
INSERT INTO public.teams VALUES (182, 'Greece');
INSERT INTO public.teams VALUES (183, 'United States');


--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.games_game_id_seq', 160, true);


--
-- Name: teams_team_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.teams_team_id_seq', 183, true);


--
-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_pkey PRIMARY KEY (game_id);


--
-- Name: teams teams_name_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.teams
    ADD CONSTRAINT teams_name_key UNIQUE (name);


--
-- Name: teams teams_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.teams
    ADD CONSTRAINT teams_pkey PRIMARY KEY (team_id);


--
-- Name: games fk_games_opponents; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT fk_games_opponents FOREIGN KEY (opponent_id) REFERENCES public.teams(team_id);


--
-- Name: games fk_games_teams; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT fk_games_teams FOREIGN KEY (winner_id) REFERENCES public.teams(team_id);


--
-- PostgreSQL database dump complete
--


When running the insert_data.sh I’m getting multiple errors like this:

ERROR:  syntax error at or near ","
LINE 1: ...ner_goals, opponent_goals) VALUES (year, 'round', , , winner...

ERROR:  syntax error at or near ","
LINE 1: ...r_goals, opponent_goals) VALUES (2018, 'Final', 208, , 4, 2)

Yeah, that appears to be coming up for me, too. However, I can’t identify any obvious culprit in my script itself, so I am unsure how to proceed.

Looking at the values that get inserted - (2018, 'Final', 208, , 4, 2) - problem appears to be with the 4th variable from ($YEAR, '$ROUND', $WINNER_ID, $OPPONENT_ID, $WINNER_GOALS, $OPPONENT_GOALS)

I realized that I wasn’t juggling my IDs appropriately, thanks.