I have the first half of the World cup database project done. In other words, all of the tables are built and ready to accept data. I’m hitting a brick wall with getting the data into the tables, I’ve tried every configuration I can remember to copy the data in from the .csv file. Nothing has worked. I have tried getting the data through a bunch of ‘if’ statements. I pulled the statements directly from the tutorial and have tried adapting them to this project. However, I’m getting nothing but syntax errors and I can’t figure out what I’m doing wrong. And I’ve only tried getting data for one column, much less all of it!!! Can anyone point me in the right direction?
year is an integer, so trying to insert a string or select a string is not going to work.
try removing the single quotes around the $YEAR in the SELECT statement first and go from there if that improves things.
#! /bin/bash
if [[ $1 == "test" ]]
then
PSQL="psql --username=postgres --dbname=worldcuptest -t --no-align -c"
else
PSQL="psql --username=freecodecamp --dbname=worldcup -t --no-align -c"
fi
# Do not change code above this line. Use the PSQL variable above to query your database.
cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
if [[ $YEAR != "year" ]]
then
# get year
YEAR=$($PSQL "SELECT year FROM games WHERE year=$YEAR")
# if not found
if [[ -z $YEAR ]]
then
# insert team_id
INSERT_YEAR_RESULT=$($PSQL "INSERT INTO games(year) VALUES($YEAR)")
if [[ $INSERT_YEAR_RESULT == "INSERT 0 1" ]]
then
echo Inserted into year, $YEAR
fi
# get new year
YEAR=$($PSQL "SELECT year FROM games WHERE year=$YEAR")
fi
fi
done
To me it looks like you are trying to read the csv file but after you read you ignore what you read and try to select instead?
Do your tables have any data?
And an echo statement for $YEAR after the select and comment out all the remaining code so the errors do not distract from the result.
If the result of the echo is not what you expect then start fixing from here.
I’m trying to read the csv file and insert the info into the sql table. I clearly didn’t understand the tutorial enough to do this project. I’m resetting the tutorial to go through it again. If I still don’t get it after that, I’m giving up.
Maybe work out a plan on how to read the file… if you have a good idea worked out on paper then you can try to implement it or look up the commands to do it.