用于加载数据文件MySQL的Bash脚本 [英] Bash Script for Load Data Infile MySQL

查看:54
本文介绍了用于加载数据文件MySQL的Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个可以运行的脚本,该脚本会将csv文件批量导入表中.

So i'm trying to create a script that I can run that will do a batch import of csv files into a table.

我无法使脚本正常工作.

I'm having trouble getting the script to work.

这是我正在运行的脚本:

Here is the script i'm running:

#!/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

运行脚本时,我收到以下错误消息:

When I run the script I receive the following error message:

./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中正常工作.

The load data local infile command works fine directly in mysql.

推荐答案

当您想在双引号字符串中使用文字双引号时,请使用 \"对其进行转义.换行,也可以换行以使其更具可读性:

When you want to use literal double quotes in double quoted strings, escape them with \". Since mysql doesn't care about line feeds, you can also break the line to make it more readable:

#!/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

这篇关于用于加载数据文件MySQL的Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆