1

Trying to write an table update statement in my bash script but gives me a syntax error mysql Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64) using readline 5.1

mysql -u UserName --password=MyPassword -D MyDatabase -e 'UPDATE MyTable SET name = SomeName WHERE number = someNumber ;'

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SomeName WHERE number = someNumber' at line 1

2
  • Does this work when using mariaDB directly? try put '' around 'SomeName' Commented Nov 30, 2020 at 0:18
  • @VladL you spotted it right away! I posted the answer below. Commented Nov 30, 2020 at 0:37

1 Answer 1

1

So the answer is: you must escape "" like so \" one liner to update mysql db from shell:

mysql -u userName --password=yourPassword -D databaseName -e "UPDATE tableName SET columnName = \"${variable}\" WHERE numberColumn = \"${numberVariable}\""
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I put two single quotes together, so it looks like a double quote. Please use single quotes in MySQL queries.
@VladL normally yes, but this is bash script so if you put single quotes over ${variable} it will be treated like a string, my version worked for me. reference for single quotes in bash: howtogeek.com/howto/29980/…
hmmm, the outer double-quotes should be sufficient to allow variable expansion (for variables referenced between the double-quotes) while also allowing for single quotes around the SQL strings, eg, "UPDATE ..... columnName = '${variable}' WHERE numberColumn = '${numberVariable}'" .. or does mariadb require double-quotes around literal values?

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.