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

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

MySQL中导出CSV格式数据的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:

  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'; 

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

  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

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

  1. 2010,"hello, line
  2. suped
  3. seped
  4. ""
  5. end"
2010,"hello, line
suped
seped
""
end"

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

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

mysql命令行导入和导出数据的更多相关文章

  1. mysql命令行导入结构化数据

    数据样本 103252765-|--|-stephanie_mt@hotmail.com-|-o/35+nGaNEU=-|-ion|-- 其中|为分隔符,每行的换行符\n mysql -uroot M ...

  2. MYSQL 命令行导入导出数据库文件

    MYSQL命令行导入数据库 1.首先通过命令行进入到mysql安装目录的bin目录下,比如我输入的命令为: cd E:\MySQL\MySQL Server 5.5\bin,输入如下命令: mysql ...

  3. 文件批量加密重命名--python脚本AND mysql命令行导入数据库

    在考试中学生交上来的报告,需要进行一下文件名加密,这样阅卷老师就不知道是谁的报告了 在百度帮助下,完成了加密和解密脚本, 加密 #!/usr/bin/python # -*- coding: utf- ...

  4. (转)MySQL命令行--导入导出数据库

    MySQL命令行导出数据库:   1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Se ...

  5. Mysql命令行导入sql数据

    mysqldump  是在  操作系统命令行下运行的,不是在 MySQL 命令行下运行的. 登陆数据库: 登陆本地mysql : mysql -h localhost -u root -p123456 ...

  6. MySQL命令行--导入导出数据库

    MySQL命令行导出数据库:   1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Se ...

  7. MySql:mysql命令行导入导出sql文件

    命令行导入 方法一:未连接数据库时方法 #导入命令示例 mysql -h ip -u userName -p dbName < sqlFilePath (结尾没有分号) -h : 数据库所在的主 ...

  8. mysql命令行导入sql脚本中文变问号问题

    之前一直用工具连接mysql虽然小问题不断也都无伤大雅,最近做金融云项目,只能通过服务器的内网访问数据库,也就是说只能在linux下通过命令行访问,在导入中文的时候发现都变成问号了,经过查询资料解决, ...

  9. MySQL命令行导入.sql文件遇到的问题

    导入.sql文件的命令行只有一句.但因为.sql文件大,在把本地的.sql文件导入到阿里云服务器的MySQL数据库时遇到了两个问题导入.sql文件的命令(假设数据库名为mydb,用户名root,密码1 ...

随机推荐

  1. Codeforces 813E Army Creation(主席树)

    题目链接  Educational Codeforces Round 22 Problem E 题意  给定一个序列,$q$次查询,询问从$l$到$r$中出现过的数字的出现次数和$k$取较小值后的和 ...

  2. Python的程序结构[8] -> 装饰器/Decorator -> 装饰器浅析

    装饰器 / Decorator 目录 关于闭包 装饰器的本质 语法糖 装饰器传入参数 1 关于闭包 / About Closure 装饰器其本质是一个闭包函数,为此首先理解闭包的含义. 闭包(Clos ...

  3. 搭建负载均衡的环境(利用虚拟机上的四台centos)

    以下转载: 准备 l 系统:Centos6  (三台) l 负载均衡:LVS  + keepalived l 服务器1:Http l 服务器2:Http ip配置 1.VIP(virtual ip): ...

  4. POJ3345 Bribing FIPA(树形DP)

    题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...

  5. [BZOJ5110]Yazid的新生舞会

    题目大意: 给你一个长度为$n(n\leq 5\times 10^5)$的序列$A_{1\sim n}$.求满足区间众数在区间内出现次数严格大于$\lfloor\frac{r-l+1}{2}\rflo ...

  6. extjs combo中给Store插入一条数据

    { xtype: 'combo', columnWidth: .55, name: 'AQLLevel', store: Ext.create('Scripts.Code.Common.store.I ...

  7. init_machine 在Kernel中被调用的过程

    以tiny4412为例: arch/arm/mach-exynos/mach-tiny4412.c MACHINE_START(TINY4412, "TINY4412") /* M ...

  8. mtd-utils 及 ubi-utils 交叉编译

    参考: http://blog.csdn.net/zjjyliuweijie/article/details/7205374 NAND是嵌入式系统一个很重要的部件,而mtd-utilts中包含了很多针 ...

  9. spring自动装配(No qualifying bean )

    No qualifying bean of type [com.wfj.service.cms.main.ChannelMng] found for dependency: expected at l ...

  10. 解决dubbo问题:forbid consumer(1)

    原因: 1.dubbo服务没有起动起来 2.dubbo链接的地址出现异常 3.dubbo服务端更新了服务接口,没有发布 如果已上都没有问题,那么还有一个原因就是 “ 别人的代码有问题 阻碍了 你的程序 ...