Code does the task, but not accepted. 'Build a Student Database: Part 1'

Question (this is around ~65% completion mark of this exercise): It’s the same as the majors, so below the second if not found comment, add an if statement that checks if the query was empty so you can insert the course if needed. Place the existing insert course and get new course_id comments in the statements area of the if.

My issue: Solution not getting accepted, but the script seems to be working as expected. This was checked by adding into empty table and then checking by running in bash: <./insert_data.sh>; then, running in sql: <SELECT * FROM COURSES>,<SELECT * FROM MAJORS>, etc. Therefore, I’m not sure what’s wrong here or what changes are expected for the solution.

My Code:

#!/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')")
    
      # get new course_id
      COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
    fi
    # insert into majors_courses

  fi
done

This issue of bash coding remains unsolved. In the meantime, Part 2 got completed without hassle! So, better hints for this task is probably required.

Yeah. The step is expecting just creating new if and moving two comments into it. Adding code corresponding to the comments is further along the road.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.