将参数传递给MySQL脚本命令行

时间:2021-08-26 00:39:12

Is there any option to pass a parameter from the command line to MySQL script?

是否有任何选项可以将参数从命令行传递到MySQL脚本?

Something like @start_date in this example:

像这个例子中的@start_date:

mysql –uuser_id -ppassword –h mysql-host -A -e 
"set @start_date=${start_date}; source ${sql_script};" >${data_file};

3 个解决方案

#1


36  

Found an answer on the net here.

在网上找到答案。

Basically, suppose that we want to run this query:

基本上,假设我们要运行此查询:

Select c_id, c_first_name,c_last_name, c_address,last_modified_date
from customer
where last_modified_date >=@start_date and last_modified_date <= @end_date;

We can pass 'start_date' and 'end_date' like this:

我们可以像这样传递'start_date'和'end_date':

/usr/bin/mysql –uuser_id -ppassword –h mysql-host -A \
    -e "set @start_date=${start_date}; set @end_date=${end_date};\
        source ${sql_script};" > ${data_file}

#2


1  

The Short answer:

简答:

bash :

bash:

        mysql -u"$mysql_user" -p"$mysql_user_pw" \
        -e "set @mysql_db='$mysql_db';source \
        00.create-mysql-starter-db.mysql ;" > "$tmp_log_file" 2>&1

mysql:

MySQL的:

     SET @query = CONCAT('CREATE DATABASE /*!32312 IF NOT EXISTS*/ `'
     , @mysql_db , '` /*!40100 DEFAULT CHARACTER SET utf8 */') ; 
     -- SELECT 'RUNNING THE FOLLOWING query : ' , @query ; 
      PREPARE stmt FROM @query;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;

The Long answer : The whole concept as a runnable bash , mysql app :

答案很长:整个概念作为一个可运行的bash,mysql app:

https://github.com/YordanGeorgiev/mysql-starter

https://github.com/YordanGeorgiev/mysql-starter

or

要么

    git clone git@github.com:YordanGeorgiev/mysql-starter.git

#3


0  

The "-e" option is sensible to the used quotes, try the double quote:

“-e”选项对使用的引号是明智的,请尝试双引号:

mysql –uuser_id -ppassword –h mysql-host -A -e 
"set @start_date=\"$start_date\"; source \"$sql_script\";" >\"$data_file\";"

#1


36  

Found an answer on the net here.

在网上找到答案。

Basically, suppose that we want to run this query:

基本上,假设我们要运行此查询:

Select c_id, c_first_name,c_last_name, c_address,last_modified_date
from customer
where last_modified_date >=@start_date and last_modified_date <= @end_date;

We can pass 'start_date' and 'end_date' like this:

我们可以像这样传递'start_date'和'end_date':

/usr/bin/mysql –uuser_id -ppassword –h mysql-host -A \
    -e "set @start_date=${start_date}; set @end_date=${end_date};\
        source ${sql_script};" > ${data_file}

#2


1  

The Short answer:

简答:

bash :

bash:

        mysql -u"$mysql_user" -p"$mysql_user_pw" \
        -e "set @mysql_db='$mysql_db';source \
        00.create-mysql-starter-db.mysql ;" > "$tmp_log_file" 2>&1

mysql:

MySQL的:

     SET @query = CONCAT('CREATE DATABASE /*!32312 IF NOT EXISTS*/ `'
     , @mysql_db , '` /*!40100 DEFAULT CHARACTER SET utf8 */') ; 
     -- SELECT 'RUNNING THE FOLLOWING query : ' , @query ; 
      PREPARE stmt FROM @query;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;

The Long answer : The whole concept as a runnable bash , mysql app :

答案很长:整个概念作为一个可运行的bash,mysql app:

https://github.com/YordanGeorgiev/mysql-starter

https://github.com/YordanGeorgiev/mysql-starter

or

要么

    git clone git@github.com:YordanGeorgiev/mysql-starter.git

#3


0  

The "-e" option is sensible to the used quotes, try the double quote:

“-e”选项对使用的引号是明智的,请尝试双引号:

mysql –uuser_id -ppassword –h mysql-host -A -e 
"set @start_date=\"$start_date\"; source \"$sql_script\";" >\"$data_file\";"