从标准输入的MySQL导入 [英] MySQL import from stdin
问题描述
我正在使用awk在stdout中生成一个csv. 有没有一种方法可以直接将内容导入mysql而无需将其保存到文件中?
I am generating a csv in stdout using awk. Is there a way to directly import that contents in mysql without putting it to file?
推荐答案
正如@xdazz的回答所述,只需使用LOAD DATA LOCAL INFILE
.我认为它是出于无知或懒惰而被否决的.快速浏览一下MySQL手册,就会发现这是一个完全可行的答案.
As the answer from @xdazz says, just use LOAD DATA LOCAL INFILE
. I assume it was downvoted out of ignorance or lazyness. A quick perusal of the MySQL manuals would have shown that to be a perfectly viable answer.
在2016年以及对于与大多数用户最相关的MariaDB,您可以这样做:
In 2016 and for MariaDB, which will be most relevant to most users, you do this:
bash
awk '{ /* My script that spits out a CSV */ }' | mysql --local-infile=1 -u user -ppassword mydatabase -e "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE mytable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"';"
bash
awk '{ /* My script that spits out a CSV */ }' | mysql --local-infile=1 -u user -ppassword mydatabase -e "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE mytable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"';"
很显然,您需要花些时间阅读手册,并根据需要将选项更改为LOAD DATA INFILE
.
Obviously, do bother to read the manual and change the options to LOAD DATA INFILE
as required to suit your specific case.
这篇关于从标准输入的MySQL导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!