execute .sql file in bash with params - mysql

I want to execute a .sql file in a bash file but I need to pass parameters to the .sql from the bash file. I think it's a simple solution but I can't figure it out.
This is what I have so far:
.SQL File
SET #columnValue = &1;
UPDATE tblTest SET Description = #columnValue;
Bash File
#!/bin/bash
columnValue=$1
mysql -uroot -ppassword testDB < SQLscript.sql $columnValue
Shell
sh myBashFile.sh "testColumnValue"
Can anyone tell me what i am doing wrong?

You need a combination of setting SQL parameters first and then loading your .sql file.
In your bash script you can use mysql with the -e option to execute an SQL statement. The same method must then be used to tell mysql to load the file instead of reading it using bash.
See this example script.
If you only want to execute a single statement, see this question.

Related

Iterating over elements of environment variable in PuTTY script file

I created a bat file where I asked it to load a putty session and passed another bat file using -m to execute all the commands necessary. In the second bat file, I have some repeated mysql commands to be executed. So I created a for loop, and wanted to pass the list of DB names as parameters to it, (which should be given along with the file I am asking to execute in the first bat file). I am not able to pass the list of DB names inside the file name using % symbol.
What is the solution for this.
SET file=%1
SET DB=%2
Putty.exe -load xyz -m %file% %DB% -t
This is my first bat file, I want to pass the DB name as list along with file. Can someone please help me achieve this?
I have some MySQL commands that are repeated for different DB's. Can I achieve that using for loop inside a bat file?
FOR %%q IN (%DB%) DO mysql -h xyzdb -e 'drop database %%q;'
This is giving a syntax error, can someone suggest where I went wrong?
The script file that you pass to putty is not a batch file. It's a list of commands for remote shell. So:
You have to use remote shell constructs and commands, not batch file commands.
You cannot refer to local environment variables.
But you can generate the file using the batch file
FOR %%q IN (%DB%) DO echo mysql -h xyzdb -e 'drop database %%q;' > script.txt
Putty.exe -load xyz -m script.txt
Also, for automation, you should use plink, not putty.
I'm not sure what you wanted to achieve by %DB% in the putty commandline.

How do I execute a lot of SQL queries without problems?

I have an SQL file with 30,000 UPDATE lines. When I upload it through phpmyadmin it freezes at a certain point and doesn't update everything.
Is there a way to execute all 30,000 lines, without problems, all at once? or do I have to go through and manually execute 200 lines at a time?
Line Example:
UPDATE `table` SET `value1`='Some text', `value2`=0 `value3`=1 WHERE id=500;
^ I have 30,000 lines like that.
PHPMyAdmin's query parsing is slow. It's much better to log into server via SSH and execute the command using mysql client:
$ mysql -uUsername -pPassword DatabaseName < script.sql
If you don't have SSH access, you can upload the sql script (via FTP, for example) and write a small PHP script that calls the command using system, exec or similar PHP function:
<?php
system('mysql -uUsername -pPassword DatabaseName < script.sql');
Then invoke the script via browser.
Make sure you use full paths to mysql (/usr/bin/mysql usually) and your script file.
If you use non-system character set, make sure you add the default_character_set option as well.

How can I run multiple Stored Procedures Files & Triggers (.sql) From MySQL Workbench

I am trying to run a set of sql files with stored procedures and triggers in my windows XAMPP environment. Some suggested to me using a batch script but I do not know how to do this in windows.
Is it possible to run all these .sql files from within MySQL Workbench? How? If not, can anyone tell me how to run a batch file within windows?
Thank you.
It seems Workbench doesn't support the command "SOURCE" so the next best thing is is (at least in windows) is to run a batch job. Simply create a new .sql file and add the full path to each .sql file like so:
Create the batch file:
In windows, the batch file can be a .sql with the sql comman SOURCE which calls the other .sql files, like so:
create run.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_delete.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_insert.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_load.sql
Open Command Line and CD to MySQL Folder
Open the command line, and cd to MySQL. If you are using XAMPP, the command/location should be something like:
cd C:\xampp\mysql\bin\
Execute the Batch File by pressing ENTER
Last, simply load mysql and run the batch file using the following command:
mysql -u root -h 127.0.0.1 my_database_name -vvv < C:\xampp\htdocs\mysite\sql\procs\run.sql
The execution above means the following:
mysql -u <username> -h <host> <database> -vvv < <batch_path_file_name>
-vvv shows all the queries being executed and the rows affected for debugging.
That's it. All .sql files mentioned in the run.sql file will be executed.

MYSQL command to execute multiple .sql files

I am wanting to write and execute a single mysql .sql file that will execute multiple other .sql files. I am looking for something equivalent to Oracle's:
#script1.sql
#script2.sql
#script3.sql
Does MYSQL have a command?
From bash or any shell, you can run as follows
cat script*.sql | mysql -u root -pYOURPASSWORD dbname
This would execute all for your sql files for 'dbname'
Cheers!!
source script1.sql;
source script2.sql;
...

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?
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.
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
echo "delete from competency_question_answer;" | mysql -hlocalhost -ur... etc.
Putting multiple sets of commands into .sql batch files works best, and you can execute multiples of these in the .bat file.