1

I'm working on fixing a database and have a script for correcting several tables. For example, this works fine:

mysql -u $U -p$P -D$D <<< 'ALTER TABLE xyz_tablename DROP columnname ;'

I'm not really a SQL ace so I'm not sure how to fix this one, and I'm running into a syntax error when I try this

mysql -u $U -p$P -D$D <<< 'ALTER TABLE drupal_url_alias CHANGE language language VARCHAR( 12 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '';'

This SQL statement works fine:

ALTER TABLE drupal_url_alias CHANGE language language VARCHAR( 12 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ''

3 Answers 3

4

The problem is that your SQL statement contains single quotes and is placed in single quotes. This confuses the shell about where one quoted string ends and a new one starts.

Try this:

mysql -u $U -p$P -D$D <<< "ALTER TABLE drupal_url_alias CHANGE language language VARCHAR( 12 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '';"

I have placed the statement in double quotes. Note that double quotes are not equivalent to single quotes (e.g. variable interpolation takes place in double quotes, but not in single quotes), but in your particular case the only difference is that single quotes after DEFAULT are kept intact.

Sign up to request clarification or add additional context in comments.

1 Comment

I figured it wasn't something simple I was missing. I had tried double quotes but in the wrong position. Cheers...
0

You can't have a single quote inside a single quote in Bash.

Comments

-2

You must use ":

mysql -u $U -p$P -D$D <<< "ALTER TABLE drupal_url_alias CHANGE language language VARCHAR( 12 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '';"

3 Comments

Um, no. There is no way to put single quotes inside a single quoted string in Bash.
+1 for offering both the generic anwser (escape the character) and the mysql (and some other RDBMS) specific method to address this (mixing quote types). The top option will work in any RDBMS I have ever used as well as being the solution for hte same issue in almost every programming language. The bottom one is easy to use in some situations
@l0b0, shame on me, you're right. Sometimes i forget that perl and shell differ in this aspect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.