Currently I am importing a database dump file sample.sql to the application. I need to import the database and append to the existing tables since the database contains same tables and fields.
How can I solve the issue? What is the command to append the imported database to existing database?
The code to import file
try {
String[] command = new String[]{"\""+this.m_MySqlPath+"mysql\"", Constants.DB_NAME, "-u" + Constants.DB_USER, "-p" + Constants.DB_PASSWORD, "-e", " source "+"\""+FileName+"\"" };
Process runtimeProcess = Runtime.getRuntime().exec(command, null, new File(this.m_MySqlPath));
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0)
{
System.out.println("DatabaseManager.restore: Restore Successfull");
}
else
{
System.out.println("DatabaseManager.restore: Restore Failure!");
}
Assume your script is “myscript.sql” and its stored in “c:\” then following command can be used to import that script to your database
This same command can be used in
Runtime.getRuntime().exec. But when i tried there is some simple issues and its not working properly.So i used another method.Before that i am assuming that your database script contain more data than in database. So even if i delete database and then import database using database script , i dont loss any data. I am just dropping(removing) current database and importing database from your database script file.
To solve above mentioned
Runtime.getRuntime().exec, i created a batch file named “ba.bat” . And stored that batch file in c: drive as “c:\bat.bat”. Content of that batch file is given belowYou can call with following syntax
ba.bat arg1 arg2 arg3 arg4where arg1=path to mysql bin folder example =c:\Program Files\MySQL\MySQL Server 5.1\bin
arg2=mysql host ip address
arg3=mysql username
arg4=mysql password
And the final java program is given below
In the above java program i am dropping database and importing database from script.
You must use
CREATE DATABASE IF NOT EXISTSin your script.From your question i felt this is what you want. Please let me know in case of any problem or above is not your expected answer.One more thing if you want to append data to existing database then please delete
from java code and also make the required column in your database table to ‘unique’ ie.. create unique index for required table columns