Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch script to issue commands to mySQL db?

I am trying to create a batch script that would connect to a mySQL database and issue a delete command:

@echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback
delete from competency_question_answer;

I will run this script providing the password as a command-line argument, but all this script does is, connects to the database, and the mysql> prompt will be shown. After I exit from mysql, the rest of the batch commands get to execute (and fail, no surprise).

How can I pass the SQL commands from the batch script to the mysql console? Is this even possible?

like image 470
Peter Perháč Avatar asked Dec 28 '25 11:12

Peter Perháč


2 Answers

You need to use command line tools. I don't know if there exists any for MySQL but for SQL there is SQLCMD and for Oracle there is OSQL.

What you can also do is something like this.

mysql -uuser -ppass < foo.sql

Where foo.sql is the commands you want to execute.

like image 122
Ólafur Waage Avatar answered Dec 31 '25 02:12

Ólafur Waage


You may need to connect multiple times:

@echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback -e delete from competency_question_answer;

Alternatively, you should be able to put all your commands in a separate file such as input.sql and use:

mysql -hlocalhost -urdfdev -p%1 rdf_feedback <input.sql
like image 36
paxdiablo Avatar answered Dec 31 '25 02:12

paxdiablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!