In bash script how to use cli argument as a variable for mysql query and capture result? - mysql

With this SQL query film_name is to be set with value of $1 coming from command line argument. And of course I want to capture the result any ideas? I want to do something like this
result=$(mysql myapp -B --column-names=0 -e "select film_id from films where film_name='$1'")
But this does not work. I think bash has a problem with variables in the SQL body

Maybe you can try something like the below, using "set" :
myarg=$1
mysql -h<host> -u<user> -p<password> <<"SQL"
set #my_arg='$myarg';
select film_id from films where film_name=#my_arg;
SQL

Related

Running mysql commands inside bash script

I am trying to execute a mysql command inside a bash script but every time I try to execute it fails for some reason. I have tried several ways and none seemed to work(e.g: <<QUERY...;QUERY)
My select is the following but I get an error:
#!/bin/bash
mysql -utesting -pMypass -hlocalhost -D test DB -e "
SELECT value FROM h6_options
where module=cloud
AND `option`=prefix;"
I get the following error.
ERROR 1064 (42000) at line 3: 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 '='prefix'' at line 5
Any help is appreciated.
You are using a multi-line SQL statement. this means you have two options:
you can push everything into one single line.
try reformatting your script to use EOF tags instead.
any option I didn't think of that the smart people here can consider...
here's an example:
mysql -u USER -pPASSWORD <<EOF
SQL_QUERY 1
SQL_QUERY 2
SQL_QUERY N
EOF
so for you, I would do this:
mysql -utesting -pMypass -hlocalhost -D test DB <<EOF
SELECT value FROM h6_options
where module=cloud
AND `option`='prefix';
EOF
Note: I don't like using the word EOF....for me, I like the word SQL or QUERY. because EOF means end of file. The code is identical, so you can use the word of choice:
mysql -utesting -pMypass -hlocalhost -D test DB <<QUERY
SELECT `value` FROM `h6_options`
where `module`='cloud'
AND `option`='prefix';
QUERY
Source: https://www.shellhacks.com/mysql-run-query-bash-script-linux-command-line/
Turns out the issue was the backticks. I had to escape them in order to not evaluate the line.
<<QUERY
SELECT value FROM h6_options
WHERE \`option\`="prefix"
AND module="cloud"
QUERY

Use variable in mysql CL

Is it possible to use shell variable in a mysql query via command line?
What I want to do is :
$ var='test'
$ mysql -e 'INSERT INTO table (text) VALUE ($var);' database
It didn't seems to work, even when I use ${var}
Any idea of how I can do something like that?
I have to stay in the shell, as I am running this through a jenkins 'execute shell'
So, with the help of the comment, I finally manage to do it.
The link explanations of quotes in mysql explain everything.
The solution :
$ var='test'
$ mysql -e "INSERT INTO table (text) VALUE ('$var');" database

fetch mysql result in batch shell

i'm trying to create a batch script to do some actions in my mysql database and some other actions with linux command, I'm trying to fetch result from my query to mysql in my script to put the result in an associative array, I've found just how to do it in simple array for one column, here is my query:
result=`mysql -h $DATABASE_HOST --user=$DATABASE_USER --password=$DATABASE_PASSWORD --skip-column-names -s -e "select id,type from $DATABASE_NAME.Media where status=0 "
I'm not an expert in batch script, sorry if it is a noob question !
result=$(mysql ...)
declare -A ary
while read id type; do
ary[$id]=$type
done <<< "$result"

How to get user input in MySQL?

Consider this simple Relation MyRel(A, B)
I wanted to display A based on value of B.
Here I would ask value of B from user. Is it possible in MySQL? How can I do it?
My query would be something like this SELECT A FROM MyRel WHERE B = [user_input].
Also Note that I am not using PHP or any other language with MySQL. I will execute this on Linux terminal.
If you are on the terminal anyway, why not write a little script around your functions and get the user input there, store it in variables and then send the mysql queries?
AFAIK there is no native method in MySQL that allows something like this, because SQL is there to query the data, not the user.
Small example:
echo "Please specify 'b'"
read b
mysql -u username -ppassword -D dbname -e "SELECT A, B FROM MyRel WHERE B = '$b'"
You could use this command:
echo "SELECT A FROM MyRel WHERE B = [user_input]" | mysql -u login -ppassword db_name
Then you get the result on standard output. You can redirect it in a file or get it in a variable if you're in a bash script.

MySQL: Use environment variables in script

Is it possible to make a sql script use a variable defined externally?
E.g. I have the following script:
UPDATE mytable
SET valid = 0
WHERE valid = 1
which I have to run through mysql command line several times, each with a different table name.
I would like something like:
SET table_name=foo
mysql -uuser -ppassword < myscript.sql
is it possible?
Skirting around the environment variables, why not:
sed 's/mytable/foo/' myscript.sql | mysql -uuser -ppassword