Relational DB Students

Hi, I got stuck here.

I run the script (see below) and it does what it is supposed to do , checking if major_id and course_id exist in each table and inserting the values in the tables looping over a test file.
but the message I get is “your script should have the suggested command added correctly”, I checked if missing parenthesis, indentation, single and double quotes but I do not get it. Any idea why is not meeting the requirements?

#!/bin/bash

# Script to insert data from courses.csv and students.csv into students database

PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"

cat courses_test.csv | while IFS="," read MAJOR COURSE
do
  if [[ $MAJOR != "major" ]]
  then
    # get major_id
    MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")

    # if not found
    if [[ -z $MAJOR_ID ]]
    then
      # insert major
      INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
      if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
      then
        echo Inserted into majors, $MAJOR
      fi

      # get new major_id
      MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
    fi

    # get course_id
    COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")

    # if not found
    if [[ -z $COURSE_ID ]]
    then
      # insert course
      INSERT_COURSE_RESULT=$($PSQL "INSERT INTO courses(course) VALUES('$COURSE')")
      if [[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]]
      then
        echo Inserted into courses, $COURSE
      fi

      # get new course_id
      COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
    fi

    # insert into majors_courses

  fi
done

this is the output but seems to be not the right script for the project

Is that your code which you have posted? The code you’ve posted is way ahead of the step which you’re stuck on.

You probably can’t pass the step because your code doesn’t match the step. The comments mentioned in this step shouldn’t have any code attached to them yet.

1 Like

yes, that’s my code, does it mean I just needed to place the if condition without the statement? lol, it worked.