mysql命令行导入和导出数据

 首先打开命令窗口,输入命令:mysql -h localhost -u selffabu -p

连接成功后,进行下面的操作

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

Sql代码
  1. select * from test_info   
  2. into outfile '/tmp/test.csv'   
  3. fields terminated by ',' optionally enclosed by '"' escaped by '"'   
  4. 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语句样本如下,要导入的文件编码格式是UTF-8:

Sql代码
  1. load data local infile '/tmp/test.csv'   
  2. into table test_info    
  3. fields terminated by ','  optionally enclosed by '"' escaped by '"'   
  4. lines terminated by '\n';
load data local infile '/tmp/test.csv' 
into table test_info  
fields terminated by ','  optionally enclosed by '"' escaped by '"' 
lines terminated by '\n'; 

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

[sql]
  1. fields terminated by ',' optionally enclosed by '"' escaped by '"'   
  2. 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]
  1. use test;  
  2.   
  3. create table test_info (  
  4.     id  integer not null,  
  5.     content varchar(64) not null,  
  6.     primary key (id)  
  7. );  
  8.   
  9. delete from test_info;  
  10.   
  11. insert into test_info values (2010, 'hello, line  
  12. suped  
  13. seped  
  14. "  
  15. end'  
  16. );  
  17.   
  18. select * from test_info;  
  19.   
  20. select * from test_info into outfile '/tmp/test.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';  
  21.   
  22. delete from test_info;  
  23.   
  24. load data infile '/tmp/test.csv' into table test_info  fields terminated by ','  optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';  
  25.   
  26. select * from test_info;  
  27.   
  28.    
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]
  1. 2010,"hello, line  
  2. suped  
  3. seped  
  4. ""  
  5. end"  
2010,"hello, line
suped
seped
""
end"

 

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

Bash代码
  1. #!/bin/sh  
  2.   
  3.   
  4. # Copyright (c) 2010 codingstandards. All rights reserved.  
  5. # file: mysql.sh  
  6. # description: Bash中操作MySQL数据库  
  7. # license: LGPL  
  8. # author: codingstandards  
  9. # email: codingstandards@gmail.com  
  10. # version: 1.0  
  11. # date: 2010.02.28  
  12.   
  13.   
  14. # MySQL中导入导出数据时,使用CSV格式时的命令行参数  
  15. # 在导出数据时使用:select ... from ... [where ...] into outfile '/tmp/data.csv' $MYSQL_CSV_FORMAT;  
  16. # 在导入数据时使用:load data infile '/tmp/data.csv' into table ... $MYSQL_CSV_FORMAT;  
  17. # CSV标准文档:RFC 4180  
  18. MYSQL_CSV_FORMAT="fields terminated by ',' optionally enclosed by '\"' escaped by '\"' lines terminated by '\r\n'"  
#!/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'

 

转自:http://blog.csdn.net/sara_yhl/article/details/6850107

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/396855.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)

http://www.jb51.net/article/15714.htm 1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。2. copy.deepcopy 深拷贝 拷贝对象及其子对象 一个很好的例子: 1 import copy2 a [1, 2, 3, 4, [a, b]] #原始对象3 4 b a #赋值&#xff0c…

7.组件连线(贝塞尔曲线)--从零起步实现基于Html5的WEB设计器Jquery插件(含源码)...

上节讲到如何创建组件,清除设计器视图,以及设计视图的持久化和恢复,本节将重点讲如何实现组件间的连线,前面章节有提到为了方便从持久化文件中恢复,组件和连线是分别存放的:nodes和lines对象,两…

linux bind命令,LINUX命令bind-系统管理-显示或设置键盘按键与其相关的功能

bind命令 用于显示和设置命令行的键盘序列绑定功能。通过这一命令,可以提高命令行中操作效率。您可以利用bind命令了解有哪些按键组合与其功能,也可以自行指定要用哪些按键组合。语法bind(选项)选项-d:显示按键配置的内容;-f&…

定位排查工作流的计算结果数据量不符合预期的方法

近期有发现一些用户在咨询,为什么数据从数据源出来后,经过了一些计算,结果不符合预期了。最常见的是说,为什么我的数据在Mysql里有xx条,怎么到MaxCompute里算了下结果变了。因为这是两个不同的系统,我们又没…

canvas 插件_基于canvas的JavaScript 二维码生成工具——QRCanvas

介绍在我们日常的开发中,特别是在现代的社会环境下,二维码的应用可谓是丰富多彩,各种各样让人眼花缭乱的二维码,可见二维码已经渗透进我们生活的方方面面,也可以说目二维码确确实实方便了我们的生活。因为作为开发人员…

spring cloud feign 上传文件报not a type supported by this encoder解决方案

上传文件调用外部服务报错: not a type supported by this encoder 查看SpringFormEncoder类的源码: 1 public class SpringFormEncoder extends FormEncoder2 {3 4 public SpringFormEncoder()5 {6 this(((Encoder) (new feign.codec.…

counter 计数器

包含了两个属性和一个方法: 1. counter-reset2. counter-increment3. counter()/counters()counter-reset(主要作用就是给计数器起个名字。如果可能,顺便告诉下从哪个数字开始计数。默认是0):.xxx { counter-reset: sm…

linux中的变量文件路径,Linux库文件和Shell可执行程序命令文件搜索路径变量的设置...

一、库文件的搜索路径:1、在配置文件/etc/ld.so.conf中指定动态库搜索路径(需要添加其它库文件的路径,在文件的最后添加具体的路径即可 [ 如:/usr/local/lib ],添加后保存退出,然后在命令行ldconfig2、通过环境变量LD_…

消息队列NetMQ 原理分析2-IO线程和完成端口

目录 前言介绍目的IO线程初始化IO线程Proactor启动Procator线程轮询处理socketIOObject总结前言 介绍 [NetMQ](https://github.com/zeromq/netmq.git)是ZeroMQ的C#移植版本,它是对标准socket接口的扩展。它提供了一种异步消息队列,多消息模式,消息过滤(订阅&#xf…

django——url(路由)配置

URL是Web服务的入口,用户通过浏览器发送过来的任何请求,都是发送到一个指定的URL地址,然后被响应。 在Django项目中编写路由,就是向外暴露我们接收哪些URL的请求,除此之外的任何URL都不被处理,也没有返回。…

VC连接mysql数据库错误:libmysql.lib : fatal error LNK1113: invalid machine 解决方法

VC连接MySQL的配置过程在上一篇博文中,不过当你设置好,以为万事大吉的时候,运行却出现这个错误:libmysql.lib : fatal error LNK1113: invalid machine type。 无效的机器类型,真的是很让人捉急。 发生这个错误的原因是…

linux 内存泄漏 定位,一种内存泄露检查和定位的方法

一个系统后台服务进程,可能包括多个线程,在生成环境下要求系统程序能够稳定长时间稳定运行而不宕机。其中一个基本的前提就是需要保证系统程序不存在内存泄露。那么,该如何判读系统程序是否存在内存泄露呢?如果存在,又…

python怎么发送邮件_在Python如何使用SMTP发送邮件

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。 python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。 Python创建 SMTP…

统计单词个数(划分型)

codevs 1040 统计单词个数 2001年NOIP全国联赛提高组 题目等级 : 黄金 Gold题目描述 Description给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入&#xff0c;且保证每行一定为20个)。要求将此字母串分成k份(1<k<40)&#xff0c…

基于ASP.NET的新闻管理系统(三)代码展示

5.1.1栏目部分 增加栏目&#xff08;addLanMu.aspx&#xff09;&#xff1a; <html xmlns"http://www.w3.org/1999/xhtml"> <head runat"server"> <title></title> <link rel"stylesheet" type"text/css" …

洛谷-求同构数的个数-NOIP2013提高组复赛

题目描述 Description 所谓同构数是指这样的数&#xff0c;即它出现在它的平方数的右端。例如&#xff0c;5的平方是25 &#xff08;即5525&#xff09;&#xff0c;5是25右端的数&#xff0c;那么5就是同构数。又如&#xff0c;25的平方是625&#xff08;即2525625&#xff09…

plex linux 数据目录,shareplex日常维护文档

2017/07/25##SharePlex日常维护源(SRC)&#xff1a;192.168.1.101 db01目标(TGT):192.168.1.102 db02SRC:su - oraclesp_ctrlshowqstatusshow capture detailshow read detailshow log reverseshow config --查看当前使用参数文件list config --罗列出所有的参数文件(使用和未使…

ifconfig命令找不到_02. Linux命令之查看网络连接

1. 查看网络连接数和端口使用 netstat 命令查看网络连接情况netstat -anp参数&#xff1a;-a 显示所有选项-t (tcp)仅显示tcp相关选项-u (udp)仅显示udp相关选项-n 拒绝显示别名&#xff0c;能显示数字的全部转化成数字。-p 显示建立相关链接的程序名关键列解释:Proto 表示协议…

grep与egrep的区别

grep与egrep的区别&#xff1b; 在linux系统环境下&#xff0c;我们通常使用grep命令来过滤出需要的行而egrep确很少使用&#xff0c;他们的区别其实很简单&#xff0c;grep默认不支持正则表达式&#xff0c;egrep默认支持正则表达式&#xff0c;egrep 等于 grep -E 命令。转载…

python学习之模块(pip),列表生成式,模块操作mysql,excel

python基础 生成式 列表生成式  格式 [表达式 for 表达式 in 迭代对象 (可加判断)] 原&#xff1a; 1 res1 [] 2 for i in range(1,5): 3   res1.append(i) 4 print(res1) 改&#xff1a; 1 res2 [i for i in range(1,5)] 2 print(res2) 字典生成式  格式 {key:value f…