Lojza Ibg
Lojza Ibg

Reputation: 658

Grails MySQL import dump

I'm trying to write a function that imports the mysql dump into the database. My code looks like this:

def sout = new StringBuffer()
def serr = new StringBuffer()
String pathToDump = "C:\\Users\\Lojza\\AppData\\Local\\Temp\\protetika-dump-temp-13393134598714336471323836046295.sql"
def process = "mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 < \"${pathToDump}\" ".execute()
process.consumeProcessOutput(sout, serr)
process.waitForOrKill(10000)
println 'sout: ' + sout // => test text
println 'serr: ' + serr

I tried this code in Grails console, but it returns me this:

mysql  Ver 14.14 Distrib 5.5.21, for Win64 (x86)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
...... (and many more lines)

I can't figure out what's wrong with the code above... When I try to execute the generated process line in cmd, it works and did the importing to database. The command I executed looks like this:

mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 < "C:\Users\Lojza\AppData\Local\Temp\protetika-dump-temp-13393134598714336471323836046295.sql"

Thanks for any help!

Regards,

Lojza

Upvotes: 0

Views: 653

Answers (1)

Jared
Jared

Reputation: 39887

Redirection does not work the same way under Java as it does with the Windows command prompt. A way to get around this is to execute the source command with the -e option to read a file. So from the command line you could do

mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 –e “source C:\Users\Lojza\AppData\Local\Temp\protetika-dump-temp-13393134598714336471323836046295.sql"

Code to do this from groovy (untested),

def process = "mysql --host=localhost --user=protetika --password=protetika --port=3306 --default-character-set=utf8 -e \"source "${pathToDump}\"\" ".execute()

Upvotes: 1

Related Questions