`
hqman
  • 浏览: 363032 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Export from MySQL to CSV

阅读更多


Using command line tools to export data from a MySQL database into a CSV file is quite easy. Here's how:

mysql -uexampleuser -pletmein exampledb -B -e "select * from \`person\`;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv

Here is some sample output of the above:

"id","username","group","password"
"1","tux","admin","5f4dcc3b5aa765d61d8327deb882cf99"
"2","tlugian","admin","5f4dcc3b5aa765d61d8327deb882cf99"
"3","saiyuki","admin","5f4dcc3b5aa765d61d8327deb882cf99"
"4","fred","staff","5f4dcc3b5aa765d61d8327deb882cf99"
"5","barney","staff","5f4dcc3b5aa765d61d8327deb882cf99"
"6","wilma","admin","5f4dcc3b5aa765d61d8327deb882cf99"

And now for the explanation:

Starting with the MySQL command. I wont explain the -u and -p options they are straight forward (if in doubt man mysql). The -B option will delimit the data using tabs and each row will appear on a new line. The -e option denotes the command to run once you have logged into the database. In this case we are using a simple SELECT statement.

Onto sed. The command used here contains three seperate sed scripts:

s/\t/","/g;s/^/"/        <--- this will search and replace all occurences of 'tabs' and replace them with a ",".

;s/$/"/;    <--- This will place a " at the start of the line.

s/\n//g    <---- This will place a " at the end of the line.

After running the result set through sed we redirect the output to a file with a .csv extension.

Regards, Bawdo2001

分享到:
评论

相关推荐

    embulk之sqlserver to mysql

    embulk guess sqlserver_to_mysql.yml -o config_sqlserver_to_mysql.yml embulk preview config_sqlserver_to_mysql.yml embulk run config_sqlserver_to_mysql.yml ``` #### 五、总结 Embulk为跨数据库的数据迁移...

    MySQL Workbench使用教程.pdf

    - **数据导入与导出**:支持多种格式的数据导入导出,如CSV、Excel等。 #### 二、安装 MySQL Workbench - **访问MySQL官方网站**:[MySQL Downloads](https://dev.mysql.com/downloads/workbench/)。 - **下载...

    test_Perl_

    标题"test_Perl_"可能暗示我们正在处理一个用Perl编写的测试脚本,而描述"export mysql data to csv"则揭示了这个脚本的核心功能——从MySQL数据库导出数据并保存为CSV格式的文件。这涉及到Perl与数据库的交互以及...

    php导出任意mysql数据库中的表去excel文件

    这段代码会创建一个CSV文件并将其下载到用户的电脑上,文件名为"export_data.csv"。`fputcsv()`函数会将数组转换为CSV格式并写入文件。 如果需要创建XLS文件,可以使用PHPExcel库,它允许你创建更复杂的Excel文件,...

    excel文件导入mysql数据库

    - **MySQL Workbench**: 使用MySQL Workbench的`Data Import/Export`功能,选择`Import from Self-Contained File`,然后选择Excel文件(需要先转换为CSV格式)。在导入设置中指定目标数据库和表。 - **命令行...

    exportDatabase:以XML或CSV格式从数据库导出表

    "exportDatabase" 是一个工具或程序,它允许用户以XML或CSV格式从数据库中导出表格数据。这两种格式广泛用于数据交换和存储,因为它们具有良好的可读性和兼容性。 XML(Extensible Markup Language)是一种标记语言...

    如何将 JSON, Text, XML, CSV 数据文件导入 MySQL?

    MySQL Workbench提供了图形化的导入导出工具,对于JSON和CSV文件,可以使用Table Data Export和Import Wizard。通过该工具,可以选择文件,配置导入参数,然后一键导入数据到数据库。 在进行数据导入时,注意处理...

    SQL Server 2012 Data Integration Recipes: Solutions for Integration Services PDF

    You'll find solutions for importing from Microsoft Office data stores such as Excel and Access, from text files such as CSV files, from XML, from other database brands such as Oracle and MySQL, and ...

    oracle使用sql脚本生成csv文件案例学习

    - **大容量CSV快速导入SQL Server**:对于大量数据,可以使用BULK INSERT,T-SQL的INSERT...EXEC或SQL Server Management Studio的Import/Export Wizard。 理解这些概念和操作,能帮助你有效地在Oracle和其他数据库...

    python连接mysql数据库导出url字段小程序

    在这个场景中,我们有一个名为`export_news_url.py`的Python脚本,它很可能是用于从MySQL数据库中提取新闻URL的工具。下面我们将详细探讨如何实现这样的功能,以及涉及到的相关知识点。 首先,我们需要安装`pymysql...

    最新MySQl-Workbench使用教程.docx

    - **导出数据**:同样在`Data Import/Export`,选择`Export to Self-Contained File`或`Export to External File`,选择需要导出的表和格式。 7. **图表和视图** - **创建视图**:在SQL Editor中编写`CREATE VIEW...

    MySQL+数据导入导出+实用技巧

    SELECT * INTO OUTFILE '/var/lib/mysql-files/emp.txt' FROM emp; ``` #### 三、数据导入:LOAD DATA INFILE ##### 3.1 基本概念 `LOAD DATA INFILE` 是一种将文件中的数据加载到 MySQL 表中的方法。它可以处理...

    php实现CSV文件导入和导出

    $result = mysql_query("SELECT * FROM student"); header("Content-Type: text/csv"); header("Content-Disposition: attachment; filename=export.csv"); $file = fopen("php://output", "w"); $header = ...

    大数据技术基础实验报告-Hive安装配置与应用.doc

    INSERT OVERWRITE DIRECTORY '/path/to/export' SELECT * FROM employees; ``` Hive的优势在于其可扩展性、容错性和对大规模数据的处理能力。然而,由于其基于MapReduce,因此对于实时查询和低延迟操作可能不太...

    Mysql中文乱码以及导出为sql语句和Excel问题解决方法[图文]

    SELECT CONVERT(column_name USING gb2312) FROM table_name INTO OUTFILE '/tmp/output.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; ``` 然后在Excel中打开CSV文件,确保Excel...

    shell脚本导出表数据并压缩文件上传指定ftp目录

    export_data=$(mysql -u user -p password -h host -D database -e "SELECT * FROM table WHERE timestamp &gt; '2022-01-01'") &gt; export.csv # 计算文件信息 file_size=$(stat -c%s export.csv) file_bytes=$(wc -c ...

    端口查看工具

    line when you export to csv or tab-delimited file. * Version 1.82: o Added 'Resize Columns On Every Refresh' option, which allows you to automatically resize the columns according to the text ...

    数据库运维形考任务4-实验4数据库备份与恢复实验.doc

    SELECT * FROM departments INTO OUTFILE '/path/to/departments.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; ``` - **注意**: - 需要确保MySQL用户有`FILE`权限。 ...

Global site tag (gtag.js) - Google Analytics