Qing
Qing

Reputation: 143

pass parameter from bash to mysql script

I'm trying to pass parameter from bash script to mysql script. The bash script is

#!/bin/bash
for file in `ls *.symbol`
do
path=/home/qz/$file
script='/home/qz/sqls/load_eval.sql'
mysql -u qz -h compute-0-10 -pabc -e "set @pred = '$path'; source $script;" 
done

The load_eval.sql is

use biogrid;
load data local infile @pred into table lasp
fields terminated by ','
lines terminated by '\n'
(score, symbols);

When running the bash script, I got error the message:

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 '@pred into table lasp ..

It seems the value of the parameter @pred is not passed into mysql script.

Upvotes: 0

Views: 2245

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562951

MySQL doesn't support session variables in a LOAD DATA INFILE statement like that. This has been recognized as a feature request for quite some time (http://bugs.mysql.com/bug.php?id=39115), but the feature has never been implemented.

I would recommend using mysqlimport instead of doing the complex steps with mysql that you're doing. The file's name must match the table's name, but you can trick this with a symbolic link:

#!/bin/bash
for file in *.symbol
do
  path="/home/qz/$file"
  ln -s -f "$path" /tmp/lasp.txt
  mysqlimport -u qz -h compute-0-10 -pabc \
    --local --columns "score,symbols" /tmp/lasp.txt 
done
rm -f /tmp/lasp.txt

PS: No need use `ls`. As you can see above, filename expansion works fine.

Upvotes: 1

Related Questions