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