在Unix中加载数据INFILE

时间:2014-05-13 08:43:04

标签: unix

我有一个脚本可以将文件加载到我的mysql db:

for file in files

do

source=$file
gunzip $source
source1=${file%*.*} #remove the gz extension

#connect to database
mysql --host=localhost --user=user --password=password mydb << EOF
LOAD DATA LOCAL INFILE $source1 INTO TABLE mytable;
EOF

done

我继续收到此错误:

第1行的错误1064(42000):您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便在&text;文本中使用正确的语法INTO TABLE mytable&#39;在第1行

我的脚本有问题吗?请帮忙。谢谢!

1 个答案:

答案 0 :(得分:1)

必须根据LOAD DATA FILE命令的syntax的要求将文件名括在单引号中。

因此你可以尝试类似(未经测试的):

for file in files
do

    source=$file
    gunzip $source
    source1=${file%*.*} #remove the gz extension

    #connect to database
    mysql --host=localhost --user=user --password=password mydb << EOF
    LOAD DATA LOCAL INFILE '$source1' INTO TABLE mytable;
EOF

done