0

This post seems to be similar but, there, a user points out "long" is a reserved key. So I think even that reserved key is causing some problem, and asking this as new question.

I need to use back-tick inside shell script to execute MySQL query. The database name contains "hyphen" in it. To treat the whole DB name as one I've enclosed it inside back-tick. But shell script uses back-tick for command substitution. So I escaped back-tick with "\". Below is the result.

CREATE TABLE reporting.details AS SELECT * FROM \`Temp-17-09-19\`.details;

But I'm getting the below error.

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'Temp-17-09-19.details' doesn't exist

It has considered the whole "db-name.table_name" (Temp-17-09-19.details) as "table" itself. I'm not getting why back-ticks are removed while executing query even after escaping them. Here my context of escaping the back-tick is only from shell script not MySQL query.

Is it doable?

Update: As asked by @shellter, below is the full shell script. And shellter, enclosing the whole query in single quote also giving same error. Also replacing back-tick with single quote is giving MySQL syntax error.

#!/bin/sh
cd `dirname $0`
ROOT_PATH=`pwd`
java -Dtalend.component.manager.m2.repository=$ROOT_PATH/../lib -Xms256M -Xmx1024M -cp .:$ROOT_PATH:$ROOT_PATH/../lib/routines.jar:$ROOT_PATH/../lib/activation.jar:$ROOT_PATH/../lib/crypto-utils.jar:$ROOT_PATH/../lib/dom4j-1.6.1.jar:$ROOT_PATH/../lib/log4j-1.2.17.jar:$ROOT_PATH/../lib/mail-1.4.jar:$ROOT_PATH/../lib/mysql-connector-java-5.1.30-bin.jar:$ROOT_PATH/projtablesync_0_1.jar: local.projtablesync_0_1.projTableSync  --context=Default  --context_param Email_To="[email protected]" --context_param Email_From="[email protected]" --context_param Email_Cc="[email protected]" --context_param smtp="smtp.gmail.com" --context_param processName="Tag" --context_param dropTempTableQry="drop table if exists reporting.details;" --context_param createTempTableQry="CREATE TABLE reporting.details AS SELECT * FROM \`Temp-17-09-19\`.details;" --context_param updateFinalTableQry="UPDATE  reporting.details AS a  INNER JOIN  reporting.details AS b  ON a.tag_id = b.tag_id AND a.seq_id = b.seq_id SET a.tag_submit_date = b.tag_submit_date, a.tag = b.tag, a.tag_update_date = b.tag_update_date, a.fail_code = b.fail_code, a.is_priority = b.is_priority, a.mobile_platform = b.mobile_platform, a.status = b.status, a.target_countries = b.target_countries, a.content_type = b.content_type, a.status_code = b.status_code, a.status_code_description = b.status_code_description;" --context_param insertFinalTableQry="INSERT INTO reporting.details SELECT a.* FROM reporting.details AS a LEFT JOIN reporting.details AS b ON a.Seq_id = b.Seq_id   AND IFNULL(a.tag_id, \"\") = IFNULL(b.tag_id, \"\") WHERE b.seq_id IS NULL;"

Thank you.

3
  • @shellter, I had to delete my previous post. Please reply here. Commented Oct 4, 2019 at 13:05
  • Ouch, that is really one long cmd. Does something much simpler work, like SELECT sysdate() from DUAL; (or the MySQL equivalent)? As I no longer have access to mySQL or any commercial DB, I won't be able to experiment further on your problem. I'd recommend changing your [shell] tag to [bash] as you'll get many more readers that search for [bash] questions. Good luck. Commented Oct 4, 2019 at 14:02
  • @shellter, Ha ha. Actually if you take a close look, there are so many queries. I have problem only here --context_param createTempTableQry="CREATE TABLE reporting.details AS SELECT * FROM \`Temp-17-09-19\`.details;". Thank you for suggesting [bash] tag. Will add that too. Commented Oct 4, 2019 at 14:29

1 Answer 1

1

You need to see how java treats these escape characters and also, '*' is a special character in bash/shell, it also need to be escaped.

You can use single quotes(') instead of double to not escape anything and make shell/bash treat the string as it is (string literal).

Eg:

--context_param createTempTableQry='CREATE TABLE reporting.details AS SELECT * FROM `Temp-17-09-19`.details;'

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

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.