The following command works fine when used on a terminal
// Terminal
mysql -h localhost -u root my_database -e "select count(*) from page;"
But when used in a groovy script (via groovyconsole) it fails to execute. Instead the mysql usage options are printed, as if some unknown command was passed to mysql.
// Groovy Console
def p1 = 'mysql -h localhost -u root my_database -e "select count(*) from page;"'.execute()
p1.text
Any one know what’s up with that?
Not sure why it’s falling over, my guess is it is the way mysql is handling the select call in quotes…
As a workaround, this works:
A nicer way of handling the output is to consume it (rather than use
text) as this should alleviate any blocking issues should your buffers fill up….Something like:
Edit:
Of course, you can always just use JDBC:
Explanation
If you write a shell script like this (and save it as
/tmp/run.sh):Then, when you run:
groovy calls the simple form of
Runtime.getRuntime.exec(), and it prints out:As you can see, the parameters get all messed up, as it separates the
selectbit into words.When you instead call:
It prints out:
As groovy calls the
String[]form ofRuntime.execso java doesn’t have to guess what the different parameters are, so keeps theselectall in one parameter.Hope this explains it 🙂