Creating A .bat File To Execute Mysql And Other Commands


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


Run mysql with -e option:



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


Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).



You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Unix). For example:



script.TXT:



@echo off
mysql -u user -p
*enter in the password*
USE databasename
SELCT * FROM table;
EXIT
echo Exit from MYSQL...


Execute previous "script" this way:



cmd < script.TXT



If you perform this test, please report the result...



Antonio



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable