【问题标题】:Parsing the output of mysqldump to create CSV files with field name headers解析 mysqldump 的输出以创建带有字段名称标题的 CSV 文件
【发布时间】:2011-08-16 13:01:49
【问题描述】:

我正在尝试编写一个 bash 脚本,该脚本将在给定本地 MySQL 数据库的名称的情况下,将其所有表中的数据导出到带有包含字段名称的标题行的 CSV 文件中。例如,如果数据库有表customersordersinventory,我想转储三个文件customers.csvorders.csvinventory.csv,包括每个文件标题中的字段名称。

修补 mysqldump,我设法生成了所有我需要的 .csv 文件,但没有字段名称标题行。该命令还在 SQL 命令中创建一组仅包含表结构的 .sql 文件。在谷歌上搜索了很多之后,我找不到任何人能够在不“重新发明轮子”并编写自己的 MySQL 转储脚本的情况下解决这个问题。 mysqldump 很棒,只是缺少这个小功能。我需要的所有字段名称都在这些 SQL 文件中,只需将它们解析出来并在每个 CSV 文件中添加一行字段名称,对吧?

我的问题:我是一个 shell 脚本新手,我不知道该怎么做。

这是我目前正在使用的 mysqldump 命令:

mysqldump --host=localhost --user=myusername --password=mypassword \
          --tab=/tmp/db/ --verbose mydatabase \
          --fields-enclosed-by=\" --fields-terminated-by=,

假设数据库 mydatabase 有表 customers ordersinventory。此命令将在目录 /tmp/db 中生成六个文件:customers.sqlcustomers.txtorders.sqlorders.txtinventory.sqlinventory.txt。 (txt 文件为 CSV 文件,我的脚本稍后将文件扩展名更改为 .csv)

.sql 文件如下所示(以客户为例):

-- MySQL dump 10.13  Distrib 5.1.54, for debian-linux-gnu (i686)
--
-- Host: localhost    Database: mydatabase
-- ------------------------------------------------------
-- Server version       5.1.54-1ubuntu4

/* (i removed some generated comments here) */;

--
-- Table structure for table `customers`
--

DROP TABLE IF EXISTS `customers`;
/* (i removed some generated comments here) */;
CREATE TABLE `customers` (
  `customer_id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(256) NOT NULL,
  `last_name` varchar(256) NOT NULL,
  `email` varchar(256) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `notes` longtext NOT NULL,
  PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1865 DEFAULT CHARSET=latin1;

/* (i removed some generated comments here) */;
-- Dump completed on 2011-05-01 13:03:02

.txt 文件看起来像您期望的 CSV 一样(“data”、“like”、“this”行),但没有字段名称标题。

如您所见,我需要的字段名称就在 .sql 文件中的“CREATE TABLE...”之后。

我正在尝试编写的理想脚本将执行以下操作:

  1. 运行上面详述的 mysqldump 命令。
  2. 遍历所有匹配 /tmp/db/*.sql 的文件,并针对每个文件:
    1. 解析出字段名称并生成“string”、“of”、“them”、“like”、“this”
    2. 在匹配的 .txt 文件中,在第一行之前插入字段名称字符串。
  3. 将所有 .txt 文件重命名为 .csv 并删除所有 .sql 文件。

有什么建议吗?我会整天修修补补,直到我弄明白为止。

【问题讨论】:

    标签: mysql bash csv mysqldump text-parsing


    【解决方案1】:
    set group_concat_max_len = 5000;
    
    set @qry = (select concat("select ",group_concat(CONCAT('''', column_name, '''' ) ), " UNION SELECT * FROM " ,table_name,  ' INTO OUTFILE ', " '/tmp/test.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'  ESCAPED BY '\"' LINES TERMINATED BY '\\n' " ) from information_schema.columns where table_schema = database() and table_name = 'spree_users');
    
    prepare stmt from @qry;
    execute stmt;
    deallocate prepare stmt;
    

    【讨论】:

      【解决方案2】:

      我找不到这样做的方法,因为:

      • 我的数据库服务器是一台远程机器,带有
      • 没有 NFS
      • 也无法写入本地共享。

      我做了一个标准的 SQL 转储并将文件 grep 为 CSV 格式。

      【讨论】:

        【解决方案3】:

        我在windows下做了一个简单的测试。

        create database if not exists test;
        
        use test;
        
        create table csv_header(
        id int not null auto_increment primary key,
        fname varchar(50),
        lname varchar(50),
        dob date)
        engine = myisam;
        
        insert into csv_header (fname,lname,dob) values 
        ('nick','smith','2000-12-05'),
        ('john','white','1990-12-05');
        
        set @str = (select concat("select * from (select ", group_concat(concat("'",column_name,"'"))," union
                    select * from ", table_name, ") as t into outfile 'd:/",table_name,".txt'
                fields terminated by ',' 
                lines terminated by '\r\n'")
                    from information_schema.columns
                    where table_schema = 'test' and table_name = 'csv_header'
                    order by ordinal_position);
        
        -- select @str;
        
        prepare stmt from @str;
        execute stmt;
        deallocate prepare stmt;
        

        这是我的 csv_header.txt 的内容:

        id,fname,lname,dob
        1,nick,smith,2000-12-05
        2,john,white,1990-12-05 
        

        如果这是您要查找的内容,那么使用游标创建存储过程很简单,该游标循环架构中的所有表并为每个表执行相同的操作。让我知道。 :)

        【讨论】:

          猜你喜欢
          • 2011-04-12
          • 2011-01-06
          • 2013-11-15
          • 2017-10-15
          • 1970-01-01
          • 1970-01-01
          • 2018-10-10
          相关资源
          最近更新 更多