views:

1130

answers:

3

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?

+1  A: 

echo "delete from competency_question_answer;" | mysql -hlocalhost -ur... etc.

pjz
seems to be going in the right direction, but I get:ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"delete from competency_question_answer;"' at line 1
Peter Perháč
the command is okay when i type it into the mysql console manually... also, how would I pass more commands? There seems to be an issue with all the double quotes
Peter Perháč
In Windows, <echo "x"> echos <"x">, not <x>, so you probably should remove the double quotes.
paxdiablo
Thanks, I decided to use a separate file for the sql commands, but your solution is also nice.
Peter Perháč
+2  A: 

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.

Ólafur Waage
thanks, tried it this way and after adding an empty line at the end of the foo.sql and removing the last semicolon (seemed to cause problems), all went through successfully.
Peter Perháč
+1  A: 

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
paxdiablo