문제

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

도움이 되었습니까?

해결책 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:]")

다른 팁

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';"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top