Question

I sometimes have $title return with quote within it. Is there any proper way to

  1. Keep it while inserting or
  2. Just simply remove it so it doesn't kick back errors?

    echo "INSERT INTO $SQL_DATABASE.$SQL_TABLE SET $SQL_FILENAME_FIELD='$OUTPUTFILENAME', title='$title', url='$OUTPUTFILENAME', cover='$OUTPUTFILENAME.jpg', server='s2', dateadd=now(), lastplayed=now()-interval 2 day, record='1000', channel='2', downloaded='1';" \ | mysql -h$SQL_HOST -u$SQL_USERNAME -p$SQL_PASSWORD $SQL_DATABASE

Was it helpful?

Solution 2

The tr command is used here to delete the ' character from title.

 title=$(echo $title | tr -d "'")

To output only alphanumeric chars, the -cd options will delete all characters which are not alphanumeric.

 title=$(echo $title | tr -cd "[:alnum:]")

OTHER TIPS

To keep the quotes you need to escape them, instead of using $title use this instead:

${title//\'/\'\'}

This will transform It's a beautiful day into It''s a beautiful day.

Here is the full command line:

echo "INSERT INTO $SQL_DATABASE.$SQL_TABLE SET $SQL_FILENAME_FIELD='$OUTPUTFILENAME', title='"${title//\'/\'\'}"', url='$OUTPUTFILENAME', cover='$OUTPUTFILENAME.jpg', server='s2', dateadd=now(), lastplayed=now()-interval 2 day, record='1000', channel='2', downloaded='1';"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top