尝试调用ShellExecute来运行Mysql和sql脚本 [英] Trying to call ShellExecute to run Mysql and a sql script

查看:184
本文介绍了尝试调用ShellExecute来运行Mysql和sql脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在Delphi应用程序中使用ShellExecute打开Mysql并运行脚本.

I've been trying to use ShellExecute from within a Delphi app to open Mysql and run a script.

ShellExecute(Handle, 'open', PWideChar(InpCommandProgram.text),
    PWideChar(commandline), nil, SW_SHOWNORMAL);

InpCommandProgram.text = 'MYSQL'
commandline = '--user=root --password=password < C:/directory/filename.sql '

filename.sql已被简化为创建数据库名称".

filename.sql has been stripped down to 'Create databasename'.

命令窗口会短暂打开并滚动一些消息-读起来太快了. 如果我打开命令窗口并输入相同的程序和命令行,它将起作用. 失败时我看不到错误消息.

A command window opens briefly and scrolls some messages - too fast to read. If I open a command window and enter the same program and commandline it works. I can't see the error messages when it fails.

我在Windows 7桌面上本地运行. 我尝试将输出定向到日志文件,但我什至没有批量获取该文件.交互成功后,我会收到一条日志.我已经尝试了所有可以想到的调整.

I'm running locally on a Windows 7 desktop. I tried directing output to a log file, but I don't even get the file in batch. I do get a log when it is successful interactively. I've tried all the tweaks I could think of.

任何有关进行方法的想法都会受到赞赏.

Any ideas on how to proceed would be appreciated.

推荐答案

当您说完全相同的命令在命令提示符下工作时,您自己说了这一点.区别在于存在命令解释器cmd.exe.为您做的是创建将输入文件输送到MySQL进程的管道.

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. 使用CreateProcess开始该过程并自行设置管道.这是一个较低的级别,涉及相当数量的Win32样板文件.如果需要的话,它确实使阻塞变得更简单,直到完成该过程为止.
  2. 获取ShellExecute来调用cmd.exe并要求cmd.exe运行MySQL并整理管道.
  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.

选项2更简单.看起来像这样:

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
);

/c切换到cmd.exe会告诉您执行命令,然后终止–这正是您想要的.

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

如果在MySQL进程完成之前需要阻塞进程,则可以切换到ShellExecuteEx.这将返回您可以等待的进程句柄.同样,它有点难操作,但可能比CreateProcess更容易,因为这会迫使您管理管道.

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.

这篇关于尝试调用ShellExecute来运行Mysql和sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆