Trying to call ShellExecute to run Mysql and a sql script

半腔热情 提交于 2019-12-02 03:04:32

You said it yourself when you said that the exact same command works at the command prompt. The difference there is the presence of a command interpreter, cmd.exe. What that does for you is create the pipe that pipes the input file into the MySQL process.

You have two obvious solutions to the problem:

  1. Use CreateProcess to start the process and set up the pipes yourself. This is a little low level and involves a fair amount of Win32 boilerplate. It does make it a little simpler to block until the process has finished, should you need to do that.
  2. Get ShellExecute to invoke cmd.exe and ask cmd.exe to run MySQL and sort out the pipes.

Option 2 is the simpler. It looks like this:

ShellExecute(
    0, 
    nil, 
    'cmd.exe', 
    '/c mysql --user=root --password=password < C:/directory/filename.sql',
    nil,
    SW_SHOW
);

The /c switch to cmd.exe tells to carry out the command and then terminate – which is just what you want here.

If you need to block your process until the MySQL process is done, then you can switch to ShellExecuteEx. That returns a process handle on which you can wait. Again it's a little harder to operate, but probably still easier than CreateProcess since that forces you to manages the pipe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!