MySQL 转储到 CSV 文本文件中,列名位于顶部?

发布于 2024-10-10 20:11:44 字数 456 浏览 8 评论 0原文

可能的重复:
如何将 MySql 表导出/转储到文本文件中,包括字段名称(也称为标题或列名称)

我使用此 SQL 片段将表转储到 CSV 文本文件中:

SELECT * FROM Brand INTO OUTFILE "e: /品牌.csv” 以 ',' 结尾的字段,以 '"' 括起 以“\n”结尾的行;

但是,此方法不会在 CSV 文件的开头添加列名称。我的问题是如何选择所有列/字段名称,就像导出表并选择“将字段名称放在第一行”时 phpMyAdmin 所做的那样。

Possible Duplicate:
How to export / dump a MySql table into a text file including the field names (aka headers or column names)

I use this SQL snippet to dump a table into CSV text files:

SELECT * FROM brand INTO OUTFILE "e:/brand.csv"
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY "\n";

However this approach doesn't add the column names at the beginning of the CSV file. My question is how to select all the column / field names as well, just like what phpMyAdmin does when you export the table and select "Put fields names in the first row".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-10-17 20:11:44

我找到了一种方法,只要您运行 MySQL 5 或更高版本,就必须手动输入这些名称。就是这样,编写为在 unix 命令行上运行的 bash 脚本:

DBNAME=<database_name>
TABLE=<table_name>

FNAME=/path/to/output/dir/$(date +%Y.%m.%d)-$DBNAME.csv

#(1)creates empty file and sets up column names using the information_schema
mysql -u <username> -p<password> $DBNAME -B -e "SELECT COLUMN_NAME FROM information_schema.COLUMNS C WHERE table_name = '$TABLE';" | awk '{print $1}' | grep -iv ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g' | tr '\n' ',' > $FNAME

#(2)appends newline to mark beginning of data vs. column titles
echo "" >> $FNAME

#(3)dumps data from DB into /var/mysql/tempfile.csv
mysql -u <username> -p<password> $DBNAME -B -e "SELECT * INTO OUTFILE '/var/mysql/tempfile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM $TABLE;"

#(4)merges data file and file w/ column names
cat /var/mysql/tempfile.csv >> $FNAME

#(5)deletes tempfile
rm -rf /var/mysql/tempfile.csv

虽然不是最优雅的解决方案,但我确信比我更了解 SQL 和/或 bash 的人可以将其压缩为一行...

它的作用是:

  1. 使用 MySQL 的信息模式创建一个带有列标题的空 CSV,
  2. 在该空 CSV 中附加一个额外的换行符,这样您的数据将开始出现一个新行,
  3. 使用非常标准的“SELECT * INTO OUTFILE...创建充满数据的 CSV 的查询
  4. 将数据文件附加到带有列标题的文件中
  5. 删除(临时)数据文件

祝你好运,如果你清理了它,请发布你的结果!

I figured out a way around having to manually enter those names as long as you're running MySQL 5 or higher. Here it is, written as a bash script for running on a unix command line:

DBNAME=<database_name>
TABLE=<table_name>

FNAME=/path/to/output/dir/$(date +%Y.%m.%d)-$DBNAME.csv

#(1)creates empty file and sets up column names using the information_schema
mysql -u <username> -p<password> $DBNAME -B -e "SELECT COLUMN_NAME FROM information_schema.COLUMNS C WHERE table_name = '$TABLE';" | awk '{print $1}' | grep -iv ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g' | tr '\n' ',' > $FNAME

#(2)appends newline to mark beginning of data vs. column titles
echo "" >> $FNAME

#(3)dumps data from DB into /var/mysql/tempfile.csv
mysql -u <username> -p<password> $DBNAME -B -e "SELECT * INTO OUTFILE '/var/mysql/tempfile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM $TABLE;"

#(4)merges data file and file w/ column names
cat /var/mysql/tempfile.csv >> $FNAME

#(5)deletes tempfile
rm -rf /var/mysql/tempfile.csv

While not the most graceful solution, i'm sure it can be compressed into a single line by someone who knows SQL and/or bash a little better than me...

What it does is:

  1. uses MySQL's information schema to create an empty CSV w/ column headers
  2. appends an extra newline to that empty CSV so your data will begin appearing a new line
  3. uses a pretty standard "SELECT * INTO OUTFILE..." query to create a CSV full of data
  4. appends the data file onto the file w/ column headers
  5. deletes the (temporary) data file

Good luck, and if you clean it up, post your results!

苏佲洛 2024-10-17 20:11:44

我想这就是你想要的。我使用的是 mysql 5.1.60,它在第一行输出字段名称。这将使用“\t”作为字段分隔符,我不知道如何要求逗号代替。

echo "SELECT * FROM brand;" | mysql -uXXX -pXXX databasename > brand.tsv

I think this does what you want. I'm using mysql 5.1.60, this outputs field names on the first line. This will use "\t" as the field separator, I'm not sure how to ask for a comma instead.

echo "SELECT * FROM brand;" | mysql -uXXX -pXXX databasename > brand.tsv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文