SOLVED - Learn SQL by Building a Student Database: Part 1 - Build a Student Database: Part 1

Tell us what’s happening:

I followed the instructions and confirmed it works, but it didn’t pass

My solution:

      # 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

Instructions:
You want a nicer message when something get’s inserted so it’s more informative. Below your INSERT_MAJOR_RESULT variable, add an if statement that checks if the variable is equal to INSERT 0 1, which was what it was printing. Use echo to print Inserted into majors, $MAJOR in the statements area of the if.

Hint: 1. Make sure to put the test value (INSERT 0 1) in double quotes since it has spaces.

What it says after running: Your script should have the suggested command added correctly

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Learn SQL by Building a Student Database: Part 1 - Build a Student Database: Part 1

Whole 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

    fi

    # get course_id

    # if not found

    # insert course

    # get new course_id

    # insert into majors_courses

  fi
done

I shouldn’t have added the quotes, so annoying…

echo Inserted into majors, $MAJOR

@PaoloDiBello

Sorry for this. I want to clarify that your solution seems to be ok either with or without quotes.

It is not your fault. When developing the tests for our exercises the developer community had to make some bold decisions for what to accept as a correct answer :pensive:. The community could not include all possible cases.

It is something you might have already noticed that some projects are failing as “false negatives”.

Please keep trying the exercises - I, as a experienced coder who has tried different learning options, could assure those exercises will help you to learn.

Come back to us when finding problems. We will do our best to guide you.

1 Like

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