Worldcup Db - Stuck!

I have the first half of the World cup database project done. In other words, all of the tables are built and ready to accept data. I’m hitting a brick wall with getting the data into the tables, I’ve tried every configuration I can remember to copy the data in from the .csv file. Nothing has worked. I have tried getting the data through a bunch of ‘if’ statements. I pulled the statements directly from the tutorial and have tried adapting them to this project. However, I’m getting nothing but syntax errors and I can’t figure out what I’m doing wrong. And I’ve only tried getting data for one column, much less all of it!!! Can anyone point me in the right direction?

Here’s my code:

Here’s the error list:

year is an integer, so trying to insert a string or select a string is not going to work.
try removing the single quotes around the $YEAR in the SELECT statement first and go from there if that improves things.

I removed the single quotes and received the long list of errors still.

#! /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.
cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
  if [[ $YEAR != "year" ]]
  then
    # get year
    YEAR=$($PSQL "SELECT year FROM games WHERE year=$YEAR")
  
    # if not found
    if [[ -z $YEAR ]]
    then
      # insert team_id
      INSERT_YEAR_RESULT=$($PSQL "INSERT INTO games(year) VALUES($YEAR)")
      if [[ $INSERT_YEAR_RESULT == "INSERT 0 1" ]]
      then
        echo Inserted into year, $YEAR
      fi

      # get new year
      YEAR=$($PSQL "SELECT year FROM games WHERE year=$YEAR")
    fi
  fi
done

The errors continue for another page or two beyond the screenshot.

What is the purpose of this code?

To me it looks like you are trying to read the csv file but after you read you ignore what you read and try to select instead?
Do your tables have any data?

And an echo statement for $YEAR after the select and comment out all the remaining code so the errors do not distract from the result.

If the result of the echo is not what you expect then start fixing from here.

I’m trying to read the csv file and insert the info into the sql table. I clearly didn’t understand the tutorial enough to do this project. I’m resetting the tutorial to go through it again. If I still don’t get it after that, I’m giving up.

have you skipped any of the projects?
It is best to work your way from top to bottom of the list as they build upon each other.

I’ve done all of them, but apparently I need to start over with Student Database tutorials. I don’t really know what else to do.

1 Like

Maybe work out a plan on how to read the file… if you have a good idea worked out on paper then you can try to implement it or look up the commands to do it.