首先打开命令窗口,输入命令: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. ssh-agent

    ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序. ssh-agent是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身 ...

  2. 【SQL】在SQL Server中多表关联查询问题

    好久没有写SQL语句的多表连接查询,总在用框架进行持久化操作.今天写了一个多表关联查询,想根据两个字段唯一确定一条数据 失败的案例如下: SELECT cyb.id,ad.name FROM [Gen ...

  3. 设计模式之迭代器模式(PHP实现)

    github地址:https://github.com/ZQCard/design_pattern/** * 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常 ...

  4. samba 服务实现在windows共享文件

    1. 什么是samba Samba服务类似于windows上的共享功能,可以实现在Linux上共享文件,windows上访问,当然在Linux上也可以访问到. 是一种在局域网上共享文件和打印机的一种通 ...

  5. Shell--变量键盘读取、数组与声明:read,array,declare

    1.read read [-pt] variable -P:后面可以接提示信息 -t:后面可以接等待的秒数,时间到后等待结束 read后面不加任何参数,直接加变量名称,那么就会主动出现一个空白行等待你 ...

  6. wget jdk

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  7. 10. 修改端口号【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637017 spring boot 默认端口是8080,如果想要进行更改的话,只需要修改 ...

  8. 在项目中引用android.support.v7

    在Android开发中,新建的项目可能因为缺少对sopport工程的引用而报错,可以这样解决. 1.项目右键 --> import --> Android --> Existing ...

  9. @classmethod, @staticmethod和@property这三个装饰器的使用对象是在类中定义的函数。下面的例子展示了它们的用法和行为:

    class MyClass(object): def __init__(self): self._some_property = "properties are nice" sel ...

  10. uva507 - Jill Rides Again(最长连续和)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=448">题目:uva507 - Jill ...