0

If I type this into command window:

"D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"

That works

Next task is running above from my own computer program...

1) Using shellexecute directly to call mysql with above parameters does not work for me. Studying SO I found that the reason is CMD doing the pipe > stuff.

2) Call shellexecute on cmd.exe wih params being

cmd /c "D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"

Delphi code example:

  S := '"D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"';
  S := '/c ' + S;
  ShellExecute(
    0,
    'open',
    'cmd.exe',
    PChar(S),
    nil,
    SW_SHOWMAXIMIZED
  );

Does not work either.

To see what the problem was, I tried putting the following into an already open command line prompt window like this:

cmd.exe /c "D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"

which gives error

'D:\databases\MySQL-5_0' is not recognized as an internal or external command, operable program or batch file.

This puzzled me as my usage of double-quotes should be correct. But trying this instead:

cmd.exe /c "mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"

still gives an error:

'mysql.exe' is not recognized as an internal or external command, operable program or batch file.

I have reached the end of the line of things to try I think.

Can anyone shed some light on how to successfully execute mysql from a program passing a commandline instruction ha instructs mysql to execute a SQL file?

Tom
  • 3,587
  • 9
  • 69
  • 124
  • 1
    CMD has special quote rules. See `cmd /?`. You are missing the last quote around the input file (CMD ignores this but it is wrong and other programs mightn't). You have to put `cmd /c`, not just `/c`. `cmd /c "D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"` –  Dec 06 '16 at 02:36
  • Both things fixed in question now - no change in result though / error messages :( – Tom Dec 06 '16 at 03:23
  • ShellExecute is a function. Should be a COM error code. Rename the folder so no brackets. –  Dec 06 '16 at 04:14
  • Typing this in command prompt which was equalant of the shellexecute does not work either: cmd.exe /c "mysql.exe" -u root betting < "W:\data\projects\temp\example.sql" (I hope solving that will also solve shellexecute usage) – Tom Dec 06 '16 at 10:16
  • You are confused, by prefixing cmd /c you no longer shellexecute - you send it to CMD. Read the Docs on CreateProcess and ShellExecute. –  Dec 06 '16 at 20:23
  • I have tried Shellexecute mysql directly (pipes not work - otherwise works fine of course, but need pipes), shellexecute cmd.exe with params 'cmd /c "D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"' and shellexecute cmd.exe with params '/c "D:\databases\MySQL-5_0(x64)\bin\mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"'. I will try understand why the manually typing in CMD window with 'cmd.exe /c "mysql.exe" -u root betting < "W:\data\projects\temp\example.sql"' fails as well. Migh help explain why shellexecute also not works. – Tom Dec 06 '16 at 23:22
  • 1
    Remove the brackets from the foldername. CMD doesn't look up the AppPaths key which shellexecute does. By passing it to CMD you loses the ability to use short names. –  Dec 06 '16 at 23:48
  • For what i is worth. I did so my software creates a .bat file on the fly containing the full paths with spaces in "", params etc. and then shellexecute that. I appreciate all the help, but I never could i working any other way for whatever the reason. @Noodles I will upvote some of your commens. Thanks again for trying to help – Tom Dec 07 '16 at 21:02

0 Answers0