title: BASH, passing variables to a string for sqlite
body: <p>Ive hit a dead end trying to pass variables created in bash to a sqlite command string.Below you can see I am trying to write the artist_id (foreign key) into the songs table in artist_id. I can retrieve th artist_id field fine from the artists table. When I try to create a new record in the songs table with the variable $artist_id, I get an error; </p>
  
  <blockquote>
    <p>Error: FOREIGN KEY constraint failed</p>
  </blockquote>
  
  <p>Its definitely the correct key, 4 in this case, but sqlite doesn't seem to be accepting it so I think it may be a formatting issue? I tried removing quotes, adding '..etc. but no joy, any ideas?</p>
  
  <pre><code>artistcheck=$(sqlite3 "$dbFile" "$forkey SELECT EXISTS(SELECT 1 FROM artists WHERE artist_name='${ARRAY[2]}' LIMIT 1);")
  if [ "$artistcheck" -eq 1 ]; then
      artistid=$(sqlite3 "$dbFile" "$forkey SELECT artist_id FROM artists WHERE artist_name='${ARRAY[2]}';")
    echo "Yep, "${ARRAY[2]}" exists and the id is $artistid."
  else
      echo "Nope, "${ARRAY[2]}" doesnt exist"
      sqlite3 "$dbFile" "$forkey INSERT INTO artists (artist_name) VALUES ('${ARRAY[2]}');"
      artistid=$(sqlite3 "$dbFile" "$forkey SELECT artist_id FROM artists WHERE artist_name='${ARRAY[2]}';")
      echo "New Artist created. Name: ${ARRAY[2]} and ID is $artistid."
  fi
  
  songcheck=$(sqlite3 "$dbFile" "$forkey SELECT EXISTS(SELECT 1 FROM songs WHERE song_name='${ARRAY[1]}' AND artist_id='$artistid' LIMIT 1);")
  if [ "$songcheck" -eq 1 ]; then
      songid=$(sqlite3 "$dbFile" "$forkey SELECT song_id FROM songs WHERE song_name='${ARRAY[1]}' AND artist_id='$artistid';")
    echo "Yep, "${ARRAY[1]}" exists and the song ID is $songid and the artist id is $artistid."
  else
    echo "Nope, "${ARRAY[1]}" doesnt exist"
    sqlite3 "$dbFile" "$forkey INSERT INTO songs (artist_id, song_name) VALUES ('$artist_id','${ARRAY[2]}');"
    songid=$(sqlite3 "$dbFile" "$forkey SELECT song_id FROM songs WHERE song_name='${ARRAY[1]}' AND artist_id='$artist_id';")
    echo "New Song created. Name: ${ARRAY[1]} and song ID is $songid and the artist id is $artistid."
  fi
  </code></pre>