views:

416

answers:

1

Hi all,

I'm new to mysql. My requirement is to create a shell script to import a sql dump file into mysql in linux and this script should be called by java program for the restoration to take on a button click.

Please advice me on this.

Regards,

Chandu.

+1  A: 

It can be done by using mysql

mysql --user=USERNAME --password=PASSWORD DATABASE < DATABASE.sql

EDIT:

To place this in a script:

file loaddb.sh:

mysql --user=USERNAME --password=PASSWORD DATABASE < $1.sql

add execute-permission by

chmod +x loaddb.sh

you would call it:

loaddb.sh YOURDBNAME

stacker
Hi stacker,Thanks for ur reply. But my requirement is to create a shell script that do this job as specified in the description.Please advice me
Chandu
Chandu, add `#!/bin/bash` as the first script line, replace `DATABASE.sql` with `$1` and you're all set.
Tomislav Nakic-Alfirevic
Hi Tomislav,Thank for your reply. I'm not clear about $1 can you explain me on this? If I specified $1 then where I have to keep dump file path for restoring.
Chandu
$1 is a placeholder for a parameter passed to you script
stacker
Chandu
chmod is only done once, to start this from java you don't need that script see http://stackoverflow.com/questions/2071682/how-to-execute-sql-script-file-in-java and accept the answer ;-)
stacker
+1 to @stacker for your patience !
gareth_bowles