-1

I'm stuck on a problem where I can't find a reasonable work around. I'm trying to run multiple commands in the Windows CMD from inside a C++ Program using

CreateProcessW(NULL,L"mysql -u root -ptoor && source C:\Users\IEUser\Documents\SWE-Software\Datenbank\datenbank-build.sql", ... );

The Problem is that when i run this code (or even when I manually put in the the CMD Window, that the second half of the command won't run since prior to this it changes the "instance?" of the CMD to that of the mysql executable.

How would I fix or work around this?

I know in python you can somewhat simulate typing in a way, which could potentially be a work around here.. But i prefer a simple solution without any external libraries.

EDIT: To clarify the first Command seems to run since I can literally see the new console text of the mysql command. Its just that the second command won't be copied in there. It just stays blank and waits for input.

EDIT: This is the Output I'm getting: enter image description here

What I do want is this:

enter image description here

SOLUTION

you can run normal statements in cmd before e.g.set PATH=\%PATH\%;

then you would use an operator like &&.

When you want to an mysql statement it has a special syntax we can use here: mysql -u username -ppassword -e "ANY VALID MYSQL STATEMENT GOES IN HERE"

e.g.: set PATH=\%PATH\%;C:\\mariadb\\bin && mysql -u root -ptoor -e "source C:\Users\IEUser\Documents\SWE-Software\Datenbank\datenbank-build.sql"

If you encounter problems with your cmd try adding an cmd /c at the start of the whole statement.

Also if mysql statement fails replace the path code with a cd mariaspath\bin, this made everything work for me.

FINAL CODE AS IT RAN IN CMD cmd /c set PATH=%PATH%;C:\Program Files\MariaDB 10.10\bin\ && mysql -u root -ptoor -e "source C:\Users\IEUser\Documents\SWE-Software\Datenbank\datenbank-build.sql" -q (note that those were ofc not hard coded but obtained at runtime.

  • 2
    The second command won't run if the first command failed. That is what the `&&` does. Related: [https://stackoverflow.com/questions/4510640/what-is-the-purpose-of-in-a-shell-command](https://stackoverflow.com/questions/4510640/what-is-the-purpose-of-in-a-shell-command) – drescherjm Nov 28 '22 at 21:06
  • 1
    it does run ^^ otherwise I wouldn't see the logged in page of my mysql db –  Nov 28 '22 at 21:08
  • Maybe you are logged into the database in some other instance. – drescherjm Nov 28 '22 at 21:09
  • 1
    @drescherjm not sure what you mean by that.. could you explain? –  Nov 28 '22 at 21:13
  • I think you were already logged in to the database and this command failed. – drescherjm Nov 28 '22 at 21:17
  • 1
    STRONG SUGGESTION: don't rely on the Windows "CMD" shell. POSSIBLE ALTERNATIVES: 1) execute the separate commands in a .bat file (or better, a PowerShell script), 2) Replace the single [CreateProcessW()](https://learn.microsoft.com/en-us/windows/win32/procthread/creating-processes) with multiple [popen()](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen) statements. – paulsm4 Nov 28 '22 at 21:23
  • 1
    @paulsm4 Overall a good suggestion, I will keep this in mind for further projects! In this I would have to create and delete this file by hand which i found to not always be as reliable as simple statements.. but this version would be way more flexible so if I can't figure it out soon, i will try powershell scripts :) –  Nov 28 '22 at 21:26
  • 1
    This answer may be what you want: [https://stackoverflow.com/a/55825350/487892](https://stackoverflow.com/a/55825350/487892) – drescherjm Nov 28 '22 at 21:46

1 Answers1

1

Update:

  • It turns out there is another problem that masked what will become a follow-on problem with &&:

    • Your mysql -u root -ptoor command enters an interactive session, which requires manual exiting before processing continues.

    • Once you make this call non-interactive, you need to apply the cmd /c fix described below.

However, based on your latest feedback it appears that you mistakenly thought that you could send the mysql command string that starts with source ... to the interactive session via &&, which cannot work. If anything, you'd have to provide such a string via stdin (echo source ... | mysql ..., for which you'd need cmd /c too), though the better option is use arguments, as shown in this answerlink courtesy of drescherjm.


Since you're trying to use a shell operator, &&, you must call via that shell's CLI, i.e. via cmd /c on Windows:

CreateProcessW(NULL,L"cmd /c mysql -u root -ptoor && source C:\Users\IEUser\Documents\SWE-Software\Datenbank\datenbank-build.sql", ... );

As noted, && by design only executes its RHS command if the LHS one signaled success (via an exit code of 0).


As for what you tried:

Without cmd /c, all arguments starting with && are passed as additional, verbatim arguments to mysql.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 2
    I will try that! This reasoning regarding the && operator makes total sense now! I will try some stuff out and update the Question :) Thank you! –  Nov 28 '22 at 21:17
  • Sadly this didn't work either :/ I tried it with a single & and && with and without cmd /c at front :/ It just keep getting the output I edited below the question! –  Nov 28 '22 at 21:35
  • @JustAnotherProgrammer, this looks like an unrelated problem: your `mysql` call is entering an _interactive session_, which blocks further processing until you _exit it manually_. – mklement0 Nov 28 '22 at 21:38
  • 1
    I don't get how this is unrelated? :D It's literally the problem and by that the question.. I would like the statement to work just as if I typed them in one after the other manually :) or any other way i can make this work.. –  Nov 28 '22 at 21:41
  • 1
    @JustAnotherProgrammer, it is unrelated in that _it isn't related to `&&`_, which is _how you framed your question_, and it is the problem that calling via `cmd /c` solves. Once you solve what is actually your primary problem, you _will_ also need the `cmd /c` fix. Please see my update. I'm not familiar with my `mysql`, but your call appears to be lacking arguments as to what query/ies should be performed. Please see my update. – mklement0 Nov 28 '22 at 21:54
  • Ok now I get what you meant! :) Another commend suggested something you also sort of did, which is an alternative syntax for the mysql statement itself! I should have thought of that myself but i didn't :D thanks for your clarification! –  Nov 28 '22 at 21:58
  • @JustAnotherProgrammer, I gather that you mistakenly thought that you could send the `mysql` _command string_ that starts with `source ...` to the interactive session via `&&`, which cannot work. If anything, you'd have to provide such a string _via stdin_ (`echo source ... | mysql ...`), though the better option is use _arguments_, as the link that drescherjm pointed you to presumably shows. I've updated the answer again. – mklement0 Nov 28 '22 at 22:07
  • @JustAnotherProgrammer, all in all, your question is an instance of the [XY problem](http://meta.stackexchange.com/a/66378/248777). – mklement0 Nov 28 '22 at 22:09