0

I'm trying to import mysql databases from a file in a groovy script.

I'm trying like this:

command = """mysql -f -u ${settings.dbUser} -p${settings.dbPassword} -h localhost --default-character-set=latin1 ${settings.dbName} < "$tableDefinitions" """

commandArray[0] = "cmd"
commandArray[1] = "/c"
commandArray[2] = "start " + command

def Process process = new ProcessBuilder(commandArray)
                                    .directory(workingDir)
                                    .redirectErrorStream(true)
                                    .start()

If I output the command on the console it looks like this:

mysql -f -u root -pHelloStackOverflow -h localhost --default-character-set=latin1 test123 < "C:\Users\XXX\git\YYY\commons\sqlData\table.sql"

However if I do this all that happens is a mysql console window opens, and I'm already logged into it, but nothing was imported. If I execute the same command on the console it works.

I tried calling mysql directly instead of cmd, but then I get the error "cannot run command", "system can't find the specified file"

How can I import like that without the mysql console opening?

2 Answers 2

1

try:

def command = [ 'cmd',
                '/c',
                'mysql -f -u ${settings.dbUser} -p${settings.dbPassword} -h localhost --default-character-set=latin1 ${settings.dbName}' ]

def process = command.execute()
proc << tableDefinitions

def out = new StringBuilder()
def err = new StringBuilder()
proc.waitForProcessOutput(out, err)
if (out) println "out:\n$out"
if (err) println "err:\n$err"
Sign up to request clarification or add additional context in comments.

5 Comments

@Opal I believe so, as it is now a single parameter to cmd -c
It solved the problem with the extra console, and now I get an output if an error occurs. But if I add the file through the pipe (process << tableDefinitions) it just hangs. But thanks so far, I'll accept the answer.
@Fels is tableDefinitions a String containing SQL?
Ahhh... try proc << tableDefinitions.text
I already tried.. The command IS executed, I can see the mysql table, waitForProcessOutput just never returns. And groovy was indeed smart enough to use .text :)
1

You need to create a list and pass every single argument separately using cmd /c add the beginning. start is not required AFAIK.

It would be:

['mysql','-f','u'] // and so on.. then call execute()

1 Comment

I think it's the redirect causing the issue

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.