Sql代码  

select * from test_info

into outfile '/tmp/test.csv'

fields terminated by ',' optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n';

select * from test_info

into outfile '/tmp/test.csv'

fields terminated by ',' optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n';

MySQL中导入CSV格式数据的SQL语句样本如下:

Sql代码  

load data infile '/tmp/test.csv'

into table test_info

fields terminated by ','  optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n';

load data infile '/tmp/test.csv'

into table test_info

fields terminated by ','  optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n';

里面最关键的部分就是格式参数

Sql代码  

fields terminated by ',' optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n'

fields terminated by ',' optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n'

这个参数是根据RFC4180文档设置的,该文档全称Common Format and MIME Type for Comma-Separated Values (CSV) Files,其中详细描述了CSV格式,其要点包括:

(1)字段之间以逗号分隔,数据行之间以\r\n分隔;

(2)字符串以半角双引号包围,字符串本身的双引号用两个双引号表示。

文件:test_csv.sql

Sql代码  

use test;

create table test_info (

id  integer not null,

content varchar(64) not null,

primary key (id)

);

delete from test_info;

insert into test_info values (2010, 'hello, line

suped

seped

"

end'

);

select * from test_info;

select * from test_info into outfile '/tmp/test.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

delete from test_info;

load data infile '/tmp/test.csv' into table test_info  fields terminated by ','  optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

select * from test_info;

use test;

create table test_info (

id  integer not null,

content varchar(64) not null,

primary key (id)

);

delete from test_info;

insert into test_info values (2010, 'hello, line

suped

seped

"

end'

);

select * from test_info;

select * from test_info into outfile '/tmp/test.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

delete from test_info;

load data infile '/tmp/test.csv' into table test_info  fields terminated by ','  optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

select * from test_info;

文件:test.csv

Text代码  

2010,"hello, line

suped

seped

""

end"

2010,"hello, line

suped

seped

""

end"

在Linux下如果经常要进行这样的导入导出操作,当然最好与Shell脚本结合起来,为了避免每次都要写格式参数,可以把这个串保存在变量中,如下所示:(文件mysql.sh)

Bash代码  

#!/bin/sh

# Copyright (c) 2010 codingstandards. All rights reserved.

# file: mysql.sh

# description: Bash中操作MySQL数据库

# license: LGPL

# author: codingstandards

# email: codingstandards@gmail.com

# version: 1.0

# date: 2010.02.28

# MySQL中导入导出数据时,使用CSV格式时的命令行参数

# 在导出数据时使用:select ... from ... [where ...] into outfile '/tmp/data.csv' $MYSQL_CSV_FORMAT;

# 在导入数据时使用:load data infile '/tmp/data.csv' into table ... $MYSQL_CSV_FORMAT;

# CSV标准文档:RFC 4180

MYSQL_CSV_FORMAT="fields terminated by ',' optionally enclosed by '\"' escaped by '\"' lines terminated by '\r\n'"

使用示例如下:(文件test_mysql_csv.sh)

Bash代码  

#!/bin/sh

. /opt/shtools/commons/mysql.sh

# MYSQL_CSV_FORMAT="fields terminated by ',' optionally enclosed by '\"' escaped by '\"' lines terminated by '\r\n'"

echo "MYSQL_CSV_FORMAT=$MYSQL_CSV_FORMAT"

rm /tmp/test.csv

mysql -p --default-character-set=gbk -t --verbose test <

use test;

create table if not exists test_info (

id  integer not null,

content varchar(64) not null,

primary key (id)

);

delete from test_info;

insert into test_info values (2010, 'hello, line

suped

seped

"

end'

);

select * from test_info;

-- select * from test_info into outfile '/tmp/test.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

select * from test_info into outfile '/tmp/test.csv' $MYSQL_CSV_FORMAT;

delete from test_info;

-- load data infile '/tmp/test.csv' into table test_info fields terminated by ','  optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';

load data infile '/tmp/test.csv' into table test_info $MYSQL_CSV_FORMAT;

select * from test_info;

EOF

echo "===== content in /tmp/test.csv ====="

cat /tmp/test.csv

mysql 导入导出 csv_学习 MySQL中导入 导出CSV相关推荐

  1. mysql 导入数据库sql语句_mysql中导入数据与导出数据库sql语句

    本文章来详细介绍关于mysql中导入数据与导出数据库sql语句,在mysql中常用的导入与导出数据的命令有source与mysqldump大家可参考. 1.例1:连接到本机上的MYSQL 首先在打开D ...

  2. mysql 烂泥,烂泥:学习mysql的binlog配置

    本文首发于烂泥行天下. 1.基础知识 日志是把数据库的每一个变化都记载到一个专用的文件里,这种文件就叫做日志文件.mysql默认只开启错误日志,因为过多的日志将会影响系统的处理性能. 在mysql5. ...

  3. 导入rasterio时的玄学错误:在命令行模式下可以导入,在代码文件中导入却会报错

    今天在一个新环境中导入rasterio时报错 ImportError: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.2 ...

  4. django excel 导出页面_Django Admin中增加导出Excel功能过程解析

    在使用Django Admin时, 对于列表我们有时需要提供数据导出功能, 如下图: 增加导出Excel功能 在Django Admin中每个模型的Admin类(继承至admin.ModelAdmin ...

  5. mysql存储animoji_iphone X Animoji怎么导入微信表情-在微信中导入Animoji表情包的方法 - 河东软件园...

    Animoji是iphone X中新增的一个功能,这个软件是用来做什么的呢?我们可以使用它来录制动图或是视频,在录制的时候会有一个小动物跟着我们的表情一起动作,最后看见自己做出的动作在小动物中一起显示 ...

  6. mysql外连接_mysql学习-mysql内连接外连接

    SQL99标准,也称为SQL1999标准 分类:内连接,外连接 一.内连接 使用  inner join ...on 语法: select 列名1 别名1,列名2 别名2... from 表名1 别名 ...

  7. linux导出日志命令_Linux中的导出命令

    linux导出日志命令 In this guide, we will look at the export command in Linux. Export is a built-in command ...

  8. python数据导出excel_Python-将数据表中数据导出到excel

    '''需求:写一个函数,随便输入一个表名,把这个表里面所有的数据,导出到excel里面 思路: 1.'select * from %s' ,查出这个表所有的数据 2.再把所有的数据写到excel xl ...

  9. mysql 如何对表排序_学习MySQL:对表中的数据进行排序和过滤

    mysql 如何对表排序 In this article, we will learn how we can sort and filter data using the WHERE clause a ...

最新文章

  1. linux裁剪—定制自己所需要的linux
  2. Linux下和Windows下创建Oracle数据库,表空间,Oracle用户
  3. H.264软件解码器在PXA270平台上的优化
  4. 云原生之容器安全实践
  5. Theory and tools for the conversion of analog to spiking convolutional neural networks
  6. vSphere 7 With K8s系列06:创建命名空间
  7. mysql from_unixtime(_MySQL 数据库中日期与时间函数 FROM_UNIXTIME(), UNIX_TIME()
  8. 在java中如何把字符串转换成时间格式?
  9. 【福利】BAT架构师分享最全Java架构师学习技能图谱:包含Java编程+网络+设计模式+数据库+分布式等
  10. 编写build.xml的12个原则
  11. 小米路由器梅林_小米路由器刷Merlin
  12. python下dicom格式图像转化为jpg格式图像
  13. 2018年访日外国游客消费创新高 中国大陆居首
  14. 周易内核与计算机原理,周易原理之我说(一)
  15. 如何获取微信小程序包
  16. linux命令如何查看dns,详解Linux系统使用dig/nslookup命令查看dns解析的方法
  17. IE标签页去掉东方头条什么的垃圾信息
  18. 机械专业夹具类毕业设计题目汇总/组合机床、车床拨叉、飞锤支架、连接座、倒挡拨叉、盖、法兰盘、铜衬轴套、心轴零件、曲轴箱零件、托板、发动机曲轴、方刀架、车床变速箱、柴油机机体、车床滤油器、方刀架……
  19. 五一节秒杀、团购 -- 商城总结
  20. 基于LAMP环境发布一个Discuz论坛网站

热门文章

  1. 网络报表学习体会心得!
  2. android studio git 错误 unable to read askpass
  3. JSP学生奖学金系统JSP学生评奖评优系统JSP奖学金管理系统 JSP奖学金评定系统
  4. 北洋UAM-05LX(网口系列适用)ROS节点
  5. 纪念日或者悼念日 网站改为灰白模式
  6. c语言判断两个字母相等,C语言:比较两个字符串是否相等
  7. 如何制作U盘启动盘来安装电脑系统
  8. hackinglab.cn脚本关之三
  9. 怎么恢复计算机系统软件,重装系统后软件如何恢复原状
  10. 解决谷歌浏览器无法输入中文的问题