Shell - one line query

Cyclone picture Cyclone · Feb 27, 2012 · Viewed 34.9k times · Source

I need to execute a mysql query in one line using bash.

It should be something like this:

mysql database --user='root' --password='my-password' < query.file

But instead of the < query.file it would like to use a raw query like this:

mysql database --user='root' --password='my-password' < UPDATE `database` SET `field1` = '1' WHERE `id` = 1111;

Is that possible?

Answer

Nishant picture Nishant · Feb 27, 2012

Did you try

 mysql -u root -pmy_password -D DATABASENAME -e "UPDATE `database` SET `field1` = '1' WHERE `id` = 1111;" > output.txt 

(the > output.txt part can be ignored but, it will be useful to see what was returned by the statement executed by looking at the file.)