Creating a .bat file to execute mysql and other commands

Solution 1:

You can run mysql in batch mode, as noted in the documentation.

mysql -h host -u user -p < batch-file

Basically you use a file containing all of your commands as an input parameter - mysql will execute the contents of that file.


Edit: If you want to build your query on the fly, you can always have your batch file write out a query to a temporary file that you can then load for execution by mysql. For example:

echo show tables from test > C:\path\to\file.sql
mysql -h host -u user -p < C:\path\to\file.sql

Solution 2:

Run mysql with -e option:

mysql -h host -u user -p -e 'SHOW TABLES FROM test'