简体   繁体   English

SQL 查询在 shell 脚本中不起作用

[英]SQL Query not working inside a shell script

I really need help on this one... so I'm writing this shell script which basically connects to MySQL database and fetches the data and give the output as a CSV file. I really need help on this one... so I'm writing this shell script which basically connects to MySQL database and fetches the data and give the output as a CSV file.

I'm able to connect to database and also able to get data from a simple query " select * from test_table; "我能够连接到数据库,也能够从简单的查询“ select * from test_table; ”中获取数据

but when I try to write this query to make the give output as csv file from script it's giving an a syntax error.但是当我尝试编写此查询以从脚本中将 output 作为 csv 文件时,它给出了语法错误。

QUERY> " select * into outfile '/Path/.cvs' fields terminated by ',' lines terminated by '\n' from test_table; " QUERY> " select * into outfile '/Path/.cvs' fields terminated by ',' lines terminated by '\n' from test_table; "

this query is not working inside the script but it is working in MySQL database (CLI).此查询在脚本中不起作用,但在 MySQL 数据库 (CLI) 中起作用。

Really need help on this guys, if there is any way around of making output as csv file do tell me otherwise helpme out on this..真的需要这些家伙的帮助,如果有任何方法可以将 output 制作为 csv 文件,请告诉我,否则请帮助我解决这个问题。

Error Msg I get is "ERROR 1064 (42000)" I know it's a syntax error but its only not working inside the script otherwise I don't how its working in mysql.我得到的错误消息是“ERROR 1064 (42000)”我知道这是一个语法错误,但它只是不能在脚本中工作,否则我不知道它在 mysql 中是如何工作的。

#!/usr/bin/bash

#scirpt to connect with db

master_db_user='root'
master_db_passwd='123'
master_db_port='3306'
master_db_host='localhost'
master_db_name='sagar_tb'


#Preparing script 

SQL_Query='select * INTO OUTFILE '/created_files/RESULT3.CSV' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' from test_table;'

#MySql Command to connect to a database 

mysql -u$master_db_user -p$master_db_passwd -P$master_db_port -h$master_db_host -D$master_db_name <<EOF
$SQL_Query
EOF
echo "End of the Script"

Really need help here guys在这里真的需要帮助

thanks and regards, Sagar Mandal谢谢和问候, Sagar Mandal

You can run any query from script using below style您可以使用以下样式从脚本运行任何查询

myaql -uroot -ppassword -hlocalhost -e 'SELECT * FROM database.table_name';

I have edited my reply according to your script我已根据您的脚本编辑了我的回复

#!/usr/bin/bash

#scirpt to connect with db

master_db_user='root'
master_db_passwd='123'
master_db_port='3306'
master_db_host='localhost'
master_db_name='sagar_tb'


#Preparing script 

SQL_Query='select * INTO OUTFILE '/created_files/RESULT3.CSV' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' from test_table;'

#MySql Command to connect to a database 

mysql -u$master_db_user -p$master_db_passwd -P$master_db_port -h$master_db_host -D$master_db_name -e '$SQL_Query';
echo "End of the Script"

The script tries to construct the command into SQL_Query.该脚本尝试将命令构造为 SQL_Query。 However, the implementation does take into account the quoting rules (you have to escape the quotes like ',')但是,实现确实考虑了引用规则(您必须转义像','这样的引号)

SQL_Query='select * INTO OUTFILE '/created_files/RESULT3.CSV' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' from test_table;'

For simple solution, just inline the SQL into the mysql command (formatted for readability).对于简单的解决方案,只需将 SQL 内联到 mysql 命令中(格式化为便于阅读)。 Using the Here document ('<使用 Here 文档 ('<

mysql ... <<EOF
select * INTO OUTFILE '/created_files/RESULT3.CSV' ...
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM