所以我正在尝试创建一个可以运行的脚本,将csv文件批量导入到表中。
我无法让脚本运行。
这是我正在运行的脚本:
#!/bin/bash
for f in *.csv
do
"/opt/lampp/bin/mysql -e use test -e LOAD DATA LOCAL INFILE ("$f") INTO TABLE temp_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (DATE, TIME, SITE_NAME, SITE_IP, TOTAL_TALKTIME, EDGE_UL_BYTES, EDGE_DL_BYTES);"
done
当我运行脚本时,收到以下错误消息:
./script.sh: line 5: unexpected EOF while looking for matching `''
./script.sh: line 7: syntax error: unexpected end of file
load data local infile命令可以直接在mysql中正常工作。
答案 0 :(得分:6)
如果要在双引号字符串中使用文字双引号,请使用\"
对其进行转义。由于mysql不关心换行符,你也可以打破这一行以使其更具可读性:
#!/bin/bash
for f in *.csv
do
/opt/lampp/bin/mysql -e "use test" -e "
LOAD DATA LOCAL INFILE '$f'
INTO TABLE temp_table
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(DATE, TIME, SITE_NAME, SITE_IP, TOTAL_TALKTIME,
EDGE_UL_BYTES, EDGE_DL_BYTES);"
done
答案 1 :(得分:1)
mysql -u<username> -p<password> -h<hostname> <db_name> --local_infile=1 -e "use <db_name>" -e"LOAD DATA LOCAL INFILE '<path/file_name>'
IGNORE INTO TABLE <table_name>
FIELDS TERMINATED BY '\t'
OPTIONALLY ENCLOSED BY '\"'"