Build A Student Database Part 1: am I stuck?

Hi everyone,
I usually don’t write my problems I encounter but I feel like this is not about me. I also Reported a Bug but I’m unsure.

The level is

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 .

I tried;

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

and tried this one too;

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

also this;

# insert course
$PSQL "INSERT INTO courses(course) VALUES('$COURSE')"
# get new course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")

But the Level is not accepting these as answers,
And the Get a Hint is not giving any answer or a hint for this either,
these codes works as intended by the way.
I m stuck

Here is all the code so far;

#!/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
  fi
  # insert into majors_courses
done

I’m just hoping that I’m missing something.
Thanks.

You might be getting a bit ahead of the test at this step. Try leaving in the then body of the if just the comments.

2 Likes

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