首先打开命令窗口,输入命令: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. 51nod 1182 完美字符串【字符串排序+哈希】

    1182 完美字符串 题目来源: Facebook Hacker Cup选拔 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 约翰认为字符串的完美度等 ...

  2. Codeforces 954H Path Counting(DP)

    题目链接  Path Counting 题意  给定一棵高度为$n$的树,给出每一层的每个点的儿子个数(某一层的所有点儿子个数相同).   令$f_{k}$为长度为$k$的路径条数,求$f_{1}, ...

  3. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- 猴子排序的期望

    链接:https://www.nowcoder.com/acm/contest/116/F来源:牛客网 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡 ...

  4. ORA-17003. Invalid column index

    sql里面有? 希望输入有参数 java程序里面没有给入参数

  5. [BZOJ3920]Yuuna的礼物

    题目大意: 给你一个长度为$n(n\le40000)$的数列$\{a_i\}(1\le a_i\le n)$,给出$m(m\le40000)$次询问,每次给出$l,r,k_1,k_2$询问区间$[l, ...

  6. Unity3d之MonoBehavior的各个函数的执行顺序,回调,顺序,次数等

    Update 当MonoBehaviour启用时,其Update在每一帧被调用.仅调用一次(每帧) LateUpdate 当Behaviour启用时,  每帧调用一次: FixedUpdate 当Mo ...

  7. 收纳箱1号 | GitHub Pages部署静态网页的一点私货

    Static site 总结各种各有的 static site generator Jekyll 其实是一个 static site generator. 如果你去 Google 这个,会发现有很多总 ...

  8. Android简单文件浏览器源代码 (转)

    Android简单文件浏览器源代码 (转) activity_main .xml <LinearLayout xmlns:android="http://schemas.android ...

  9. 给 DiscuzX3 缩略图添加水印

    Discuz X3 默认开启缩略图的时候水印只添加到原图上面,而缩略图上面无法进行水印图的添加,需要改下程序,方可给缩略图添加水印,需要修改2个地方: 1.打开 source\function\fun ...

  10. MathType requires a newer version of MT Extra等MathType问题的不兼容性解决方案

    常见问题解决方法: 1.MathType 6.0与office 2007兼容问题 由于Office软件安装时默认是不安装公式编辑器的,在安装完MathType 6.0之后,需要将\MathType 6 ...