-1

I'm building a tool using Delphi and MySQL to restore a script generated with MySQLDump. It was supposed to load and execute a SQL file and log any possible errors into a given output file.

I thought about execute the mysql command line and send command lines but i don't know if its possible ou how to do it since I just know how to call mysql using windows cmd and execute a single command line using ShellExecute or CreateProcess I tried to do it with a single command line but it did'nt logged the errors properly

I tried this: cmd /c mysql.exe --user root < "C:\restore.sql" > "C:\restore_log.txt"

the content of restore.sql was: drop database test;

It does execute my script, but on the second attempt it should log "database doesn't exist" but restore_log.txt was empty

It would help if anyone could point the way to call mysql and send multiple lines OR a help with my cmd line to log properly Anyone can help me?

luix10
  • 49
  • 8

2 Answers2

0

I don't know what components you've got available to connect to the MySQL db, but using TADOQuery to access SQL Server, I simply load up the queries into a TADOQuery's .SQL property and then call Open or ExecSQL. As long as it's just vanilla SQL that was generated by SQLDump, I'd guess that should work. It's worth a try anyway.

David Schwartz
  • 1,756
  • 13
  • 18
  • I was avoiding to use connection components.. I was trying to do it just calling mysqldump and mysql command lines, but I guess this is going to be the solution for now – luix10 Mar 10 '17 at 20:24
  • Why insist on using the command-line tool? You have no control over a separate EXE launched as a child process. Either it succeeds or fails. It could even dump duplicate data into your DB. – David Schwartz Mar 10 '17 at 23:55
  • Does this help? http://stackoverflow.com/questions/15031225/trying-to-call-shellexecute-to-run-mysql-and-a-sql-script – David Schwartz Mar 10 '17 at 23:58
  • almost that David Schwartz, this was just like my best attempt like i mentioned on the original question... the problem was that i couldn't retrieve error messages to a log file or screen – luix10 Mar 13 '17 at 12:58
-1

digging more at stackoverflow I found previous answers that helped, the difference is that the output goes to a Memo that I can save to a file. Thank you all for the help and the insights.

The answers can be found on the following links:

Getting output from a shell/dos app into a Delphi app

How do I run a command-line program in Delphi?

Getting output from a shell/dos app into a Delphi app

Community
  • 1
  • 1
luix10
  • 49
  • 8