首先打开命令窗口,输入命令: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. SpringBoot日志管理

    一.简介 小张:开发一个大型系统:1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件?2.框架来记录系统的一些运行时信息:日志框架 : z ...

  2. POJ 1044: Date bugs

    题目描述 There are rumors that there are a lot of computers having a problem with the year 2000. As they ...

  3. 网络防嗅探工具SniffJoke

    网络防嗅探工具SniffJoke   在渗透测试中,通过网络嗅探,可以获取网络通信主机的各种信息.为了防止嗅探,Kali Linux提供了专用工具SniffJoke.该工具能够自动对用户的网络数据进行 ...

  4. Codeforces Round #325 (Div. 2) Phillip and Trains dp

    原题连接:http://codeforces.com/contest/586/problem/D 题意: 就大家都玩过地铁奔跑这个游戏(我没玩过),然后给你个当前的地铁的状况,让你判断人是否能够出去. ...

  5. AOJ 0531:Paint Color(二维离散+imos)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0531 [题目大意] 给出一张图,和一些矩形障碍物,求该图没被障碍物覆 ...

  6. php的一些语法

    命名空间: 一个类为App/Http/Controllers/Controller,则该类的命名空间为App/Http/Controllers,可以通过use关键字导入该类,也可以导入命名空间,但是该 ...

  7. zookeeper 学习笔记2

    ephemeral 英[ɪˈfemərəl]美[ɪˈfɛmərəl]adj. 朝生暮死; 短暂的,瞬息的; 朝露; 一年生; ZooKeeper Watcher 机制 集群状态监控示例 为了确保集群能 ...

  8. 使用Jenkins搭建iOS开发的CI服务器

    目录 简介     下载并运行     Jenkins配置         安装git插件         E-mail设置     自动化构建         远程仓库设置         触发条件 ...

  9. Linux学习之一-从三个重要人物的故事和一张思维导图说起

    Linux是一套自由加开放源代码的类Unix操作系统,诞生于1991年10月5日(第一次正式向外公布),由芬兰学生Linus Torvalds和后来陆续加入的众多爱好者共同开发完成. Linux是一个 ...

  10. Centos 6安装 Jenkins

    前言 持续集成的概念 持续集成,Continuous integration ,简称CI. 持续集成正是针对这一类问题的一种软件开发实践.它倡导团队开发成员必须经常集成他们的工作,甚至每天都可能发生多 ...