与delphi一起使用mysql-commande行

时间:2018-10-09 15:07:06

标签: mysql delphi

大家好,我是第一次从事大型项目;我想创建一个智能程序,但是这段代码有一些大问题。

program base;
{$mode delphi}
uses
crt,
Windows,   // for constant SW_NORMAL
ShellApi;  // for function ShellExecute
 var
  comand,nomdb:widestring;
 procedure createdatabase(var comand,nomdb:widestring);

 begin

 writeln('Put the name of database');
 readln(nomdb);
 comand:='C:\AppServ\MySQL\bin\mysql.exe -uroot -ptest1234 -e "create database hello ;"';
 ShellExecute(0, 'open', PWideChar(comand), nil, nil, SW_NORMAL);

 end;



 begin
  createdatabase(comand,nomdb);
 end.

运行代码并在cmd中键入此命令时,我无法创建数据库

  

C:\ AppServ \ MySQL \ bin \ mysql.exe -uroot -ptest1234 -e“创建数据库   你好;“

然后我可以创建数据库,所以我想我的代码有问题,但是我不知道它在哪里!

1 个答案:

答案 0 :(得分:1)

您需要分别传递可执行文件路径和参数:

command:= 'C:\AppServ\MySQL\bin\mysql.exe';
parameters:= '-uroot -ptest1234 -e "create database hello ;"';
ShellExecute(0, 'open', PWideChar(command), PWideChar(parameters), nil, SW_NORMAL);

您将遇到另一个问题:ShellExecute将立即返回,而不会等到命令实际完成创建数据库的过程。如果需要,请使用CreateProcess代替How can I wait until an external process has completed?

中的Shellexecute