Creating a .bat file to execute thesql and other commands

batch fileMySQL

I'm looking for a way to reduce the amount of typing I do to check mysql entries and other things.
For example, I wanted to create a .bat file that will execute the following commands.

mysql -u user -p
*enter in the password*
USE databasename
SELCT * FROM table;

The problem is after the initial mysql -u user -p the rest of the commands written down do not get executed. Is it possible to continue running commands after calling something like mysql or other programs, that seem to add its prefix(?) to the beginning of commands (mysql>). I'm not very familiar with the terminology so forgive me if it sounds confusing. I'd like to use the same kind of concept for other things as well.

Help is much appreciated,
Thanks.

Best Answer

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
Related Question