Periodic table issue




Hello everyone! My issue is when i’m using atomic_number in WHERE other OR conditions don’t work and getting an error of column doesn’t exist, but if i delete OR atomic_number = $1 the other 2 OR conditions work just fine…I don’t understand where my mistake is…If I do the whole query in the psql command line all conditions together work just fine…Thank you in advance!

element.sh

#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=periodic_table --no-align --tuples-only -c”

if [[ -z $1 ]]

then

echo -e “Please provide an element as an argument.”

else

ELEMENT=$($PSQL "SELECT elements.atomic_number, name, symbol, types.type, atomic_mass, melting_point_celsius, boiling_point_celsius

FROM elements FULL JOIN properties USING(atomic_number) FULL JOIN types USING(type_id) WHERE ‘$1’ = name OR ‘$1’ = symbol OR $1 = atomic_number;")

echo $ELEMENT | while IFS=“|” read ATOMIC_NUMBER NAME SYMBOL TYPE ATOMIC_MASS MELTING_POINT_CELSIUS BOILING_POINT_CELSIUS

do

if [[ -z $ELEMENT ]]

then

echo -e “I could not find that element in the database.”

else

echo “The element with atomic number $ATOMIC_NUMBER is $NAME ($SYMBOL). It’s a $TYPE, with a mass of $ATOMIC_MASS amu. $NAME has a melting point of $MELTING_POINT_CELSIUS celsius and a boiling point of $BOILING_POINT_CELSIUS celsius.”

fi

done

fi

rebuild.sh

#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=periodic_table --no-align --tuples-only -c”

#1

echo $($PSQL “ALTER TABLE properties RENAME weight TO atomic_mass;”)

#2

echo $($PSQL “ALTER TABLE properties RENAME melting_point TO melting_point_celsius;”)

#3

echo $($PSQL “ALTER TABLE properties RENAME boiling_point TO boiling_point_celsius;”)

#4

echo $($PSQL “ALTER TABLE properties ALTER COLUMN melting_point_celsius SET NOT NULL;”)

#5

echo $($PSQL “ALTER TABLE properties ALTER COLUMN boiling_point_celsius SET NOT NULL;”)

#6

echo $($PSQL “ALTER TABLE elements ADD UNIQUE(name, symbol);”)

#7

echo $($PSQL “ALTER TABLE elements ALTER COLUMN name SET NOT NULL;”)

#8

echo $($PSQL “ALTER TABLE elements ALTER COLUMN symbol SET NOT NULL;”)

#9

echo $($PSQL “ALTER TABLE properties ADD FOREIGN KEY(atomic_number) REFERENCES elements(atomic_number);”)

#10

echo $($PSQL “CREATE TABLE types();”)

#11

echo $($PSQL “ALTER TABLE types ADD COLUMN type_id SERIAL PRIMARY KEY;”)

#12

echo $($PSQL “ALTER TABLE types ADD COLUMN type VARCHAR(30) NOT NULL;”)

#13

echo $($PSQL “INSERT INTO types(type) VALUES (‘metal’),(‘nonmetal’),(‘metalloid’);”)

#14

echo $($PSQL “ALTER TABLE properties ADD COLUMN type_id INT;”)

#15

echo $($PSQL “ALTER TABLE properties ADD FOREIGN KEY(type_id) REFERENCES types(type_id);”)

#16

echo $($PSQL “UPDATE elements SET symbol = INITCAP(symbol);”)

#17

echo $($PSQL “ALTER TABLE properties ALTER COLUMN atomic_mass TYPE DECIMAL;”)

#18

echo $($PSQL “UPDATE properties SET atomic_mass = atomic_mass::REAL;”)

#19

echo $($PSQL “INSERT INTO elements(atomic_number, name, symbol) VALUES (9, ‘Fluorine’, ‘F’),(10, ‘Neon’, ‘Ne’);”)

#20

echo $($PSQL “INSERT INTO properties(atomic_number, type, atomic_mass, melting_point_celsius, boiling_point_celsius) VALUES (9, ‘nonmetal’, 18.998, -220, -188.1),(10, ‘nonmetal’, 20.18, -248.6, -246.1);”)

#21

echo $($PSQL “UPDATE properties SET type_id = 1 WHERE type = ‘metal’;”)

#22

echo $($PSQL “UPDATE properties SET type_id = 2 WHERE type = ‘nonmetal’;”)

#23

echo $($PSQL “UPDATE properties SET type_id = 3 WHERE type = ‘metalloid’;”)

#24

echo $($PSQL “ALTER TABLE properties ALTER COLUMN type_id SET NOT NULL;”)

#25

echo $($PSQL “ALTER TABLE properties DROP COLUMN type;”)

#26

echo $($PSQL “DELETE FROM properties WHERE atomic_number = 1000;”)

#27

echo $($PSQL “DELETE FROM elements WHERE atomic_number = 1000;”)