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

[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. # 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命令导入导出数据_mysql命令行导入和导出数据

    MySQL中导出CSV格式数据的SQL语句样本如下: select * from test_info into outfile '/tmp/test.csv' fields terminated by ...

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

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

  3. MySQL命令行导入导出文件(全)

    文章目录 命令行导出数据库相关表为SQL文件 导出数据库表结构 导出数据库表结构和数据 命令行导入SQL文件到MySQL 命令行登陆到MySQL并选择要使用的数据库 将SQL文件进行导入 命令行直接导 ...

  4. MySQL命令行导入含空值的orderinfo和userinfo字段内容

    导入背景 在网上找到一个七周成为数据分析师的教学课程,在学到MySQL时,要导入两个很大的表:order_info_utf.csv和user_info_utf.csv,这两个表数据比较多,有几十万条数 ...

  5. MySQL命令行导入数据库

    文章目录 前言 使用命令行导入 第一步:进入MySQL的bin目录下 第一种 第二种 第二步:新建数据库并使用 使用数据库 第三步:选中导入路径 查看表是否存在 打开VS Code 注意 总结 命令行 ...

  6. mysql命令行导入sql文件

    1.首先登录到mysql命令行 2.然后选择数据库 3.设置字符编码 4.导入sql文件 # 选择数据库 mysql>use dbtest; # 设置字符编码 mysql>set name ...

  7. MySQL命令行导入导出sql文件

    linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): 1.导出数据和表结构(常用): mysqldump -u用户名 -p密码 数据库名 > 数据库 ...

  8. Ubuntu 20.04 MySQL 命令行导入导出数据库

    前言 Ubuntu(Linux)下没有像Navicat等好用的MySQL可视化工具,尤其是Server版本,没有界面,那么我们的所有操作都依赖于命令行. 参考链接:https://www.runoob ...

  9. linux mysql命令行导入_在linux中导入sql文件的方法分享(使用命令行转移mysql数据库)...

    因导出sql文件 在你原来的网站服务商处利用phpmyadmin导出数据库为sql文件,这个步骤大家都会,不赘述. 上传sql文件 前面说过了,我们没有在云主机上安装ftp,怎么上传呢? 打开ftp客 ...

最新文章

  1. 深度学习巨头Yoshua Bengio清华演讲: 深度学习通往人类水平人工智能的挑战
  2. 寻找调用DebugPort的函数
  3. JAVA字符串数学公式运算-辅助类-支持浮点数错误纠正-低消耗-高可用性-小数点后面保留16位小数
  4. C#浅拷贝与深拷贝区别
  5. Hession矩阵与牛顿迭代法
  6. javascript的内置对象
  7. tp3.2 find带参数查询及getField(两个参数)
  8. 欧洲杯第一周的比赛闲聊
  9. Suse linux 命令行
  10. Sphinx 之 Coreseek、Sphinx-for-chinaese、Sphinx+Scws 评测
  11. 腾讯云主机Ubuntu之服务器环境搭建以及宝塔面板安装
  12. Doc2Vec句向量模型PV-DM与PV-DBOW的理解
  13. 如何在基于python的聊天室中实现表情接收功能
  14. 选择面向 USB4 数据线的 ESD 保护
  15. pyqt5+opencv-python打开摄像头(已实现)
  16. php时间戳转换成日期格式,php时间戳如何转换为日期格式
  17. 教你如何设置u盘启动安装系统
  18. 西南大学计算机维修技术0240答案,西南大学17秋0240《计算机维修技术》作业答案汇总.doc...
  19. 设计模式:积分兑换系统的设计与实现
  20. 【算法图解】【读书笔记】

热门文章

  1. ffplay 分析(音频从Frame(解码后)队列取数据到SDL输出)
  2. Linux内核设计与实现---内核同步方法
  3. 模拟实现priority_queue优先级队列
  4. Ubuntu下QT的安装详细教程
  5. 利用mysql提供的c语言接口操作数据库
  6. VirtualBox Ubuntu个人配置
  7. 437. 路径总和 III
  8. Java开发中 Double 和 float 不能直接运算
  9. spring自动注入--------
  10. 读书笔记--模板与泛型编程