3

Hi I am trying to run MySQL queries from shell script.

mysql -u root -p'1234' -e "CREATE TABLE $DB.aa_vv_cc
(
id int(10) unsigned NOT NULL AUTO_INCREMENT,
city varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
lat varchar(255) DEFAULT NULL,
`long` varchar(255) DEFAULT NULL,
 status int(11) NOT NULL DEFAULT '1',
 created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY ('id')
);"

I am getting error on "long".The back tick is not working from shell script.Any help will be appreciated.

2
  • As Long is a reserved word in mysql you should not use it as column name. Commented Sep 8, 2016 at 11:07
  • Don't know why the hell it's downvoted. Hey arun, did you find the solution? Commented Oct 4, 2019 at 12:00

2 Answers 2

4

Backticks are Command Substitution in the shell.

And they are evaluated in double quoted strings.

So the shell is seeing the

`long`

in your string and trying to run the command long and, presumably, failing.

You need to escape the backticks

\`long\`

in the double quoted string to prevent that (or use a single quoted string which doesn't evaluate them)

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

1 Comment

This didn't work in my case. I had to use this with Database name like CREATE TABLE reporting.details AS SELECT * FROM \`Temp-17-09-19\`.details;. But it gave me error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'Temp-17-09-19.details' doesn't exist. So it's considering whole db.tablename as a tablename.
0

You have to try this here i have modify two things 'long' replace by \'long\' and PRIMARY KEY ('id') replace by PRIMARY KEY (id) this query is work in my shell script.Here the test is the name of database.

mysql -u root  -p'1234' -e "USE test;CREATE TABLE $DB.aa_vv_cc (id int(10) unsigned NOT NULL AUTO_INCREMENT,city varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,lat varchar(255) DEFAULT NULL,\`long\`varchar(255) DEFAULT NULL, status int(11) NOT NULL DEFAULT '1', created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamp NOT NULL DEFAULT '0000-00-0000:00:00',PRIMARY KEY (id));"

Comments

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.