mysql命令行导入和导出数据
首先打开命令窗口,输入命令:mysql -h localhost -u selffabu -p
连接成功后,进行下面的操作
MySQL中导出CSV格式数据的SQL语句样本如下:
- select * from test_info
- into outfile '/tmp/test.csv'
- fields terminated by ',' optionally enclosed by '"' escaped by '"'
- 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:
- load data local infile '/tmp/test.csv'
- into table test_info
- fields terminated by ',' optionally enclosed by '"' escaped by '"'
- 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';
里面最关键的部分就是格式参数
- fields terminated by ',' optionally enclosed by '"' escaped by '"'
- 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
- 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;
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
- 2010,"hello, line
- suped
- seped
- ""
- end"
2010,"hello, line suped seped "" end"
在Linux下如果经常要进行这样的导入导出操作,当然最好与Shell脚本结合起来,为了避免每次都要写格式参数,可以把这个串保存在变量中,如下所示:(文件mysql.sh)
- #!/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'"
#!/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命令行导入和导出数据相关推荐
- mysql命令导入导出数据_mysql命令行导入和导出数据
MySQL中导出CSV格式数据的SQL语句样本如下: select * from test_info into outfile '/tmp/test.csv' fields terminated by ...
- (转)MySQL命令行--导入导出数据库
MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serv ...
- MySQL命令行导入导出文件(全)
文章目录 命令行导出数据库相关表为SQL文件 导出数据库表结构 导出数据库表结构和数据 命令行导入SQL文件到MySQL 命令行登陆到MySQL并选择要使用的数据库 将SQL文件进行导入 命令行直接导 ...
- MySQL命令行导入含空值的orderinfo和userinfo字段内容
导入背景 在网上找到一个七周成为数据分析师的教学课程,在学到MySQL时,要导入两个很大的表:order_info_utf.csv和user_info_utf.csv,这两个表数据比较多,有几十万条数 ...
- MySQL命令行导入数据库
文章目录 前言 使用命令行导入 第一步:进入MySQL的bin目录下 第一种 第二种 第二步:新建数据库并使用 使用数据库 第三步:选中导入路径 查看表是否存在 打开VS Code 注意 总结 命令行 ...
- mysql命令行导入sql文件
1.首先登录到mysql命令行 2.然后选择数据库 3.设置字符编码 4.导入sql文件 # 选择数据库 mysql>use dbtest; # 设置字符编码 mysql>set name ...
- MySQL命令行导入导出sql文件
linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): 1.导出数据和表结构(常用): mysqldump -u用户名 -p密码 数据库名 > 数据库 ...
- Ubuntu 20.04 MySQL 命令行导入导出数据库
前言 Ubuntu(Linux)下没有像Navicat等好用的MySQL可视化工具,尤其是Server版本,没有界面,那么我们的所有操作都依赖于命令行. 参考链接:https://www.runoob ...
- linux mysql命令行导入_在linux中导入sql文件的方法分享(使用命令行转移mysql数据库)...
因导出sql文件 在你原来的网站服务商处利用phpmyadmin导出数据库为sql文件,这个步骤大家都会,不赘述. 上传sql文件 前面说过了,我们没有在云主机上安装ftp,怎么上传呢? 打开ftp客 ...
最新文章
- 深度学习巨头Yoshua Bengio清华演讲: 深度学习通往人类水平人工智能的挑战
- 寻找调用DebugPort的函数
- JAVA字符串数学公式运算-辅助类-支持浮点数错误纠正-低消耗-高可用性-小数点后面保留16位小数
- C#浅拷贝与深拷贝区别
- Hession矩阵与牛顿迭代法
- javascript的内置对象
- tp3.2 find带参数查询及getField(两个参数)
- 欧洲杯第一周的比赛闲聊
- Suse linux 命令行
- Sphinx 之 Coreseek、Sphinx-for-chinaese、Sphinx+Scws 评测
- 腾讯云主机Ubuntu之服务器环境搭建以及宝塔面板安装
- Doc2Vec句向量模型PV-DM与PV-DBOW的理解
- 如何在基于python的聊天室中实现表情接收功能
- 选择面向 USB4 数据线的 ESD 保护
- pyqt5+opencv-python打开摄像头(已实现)
- php时间戳转换成日期格式,php时间戳如何转换为日期格式
- 教你如何设置u盘启动安装系统
- 西南大学计算机维修技术0240答案,西南大学17秋0240《计算机维修技术》作业答案汇总.doc...
- 设计模式:积分兑换系统的设计与实现
- 【算法图解】【读书笔记】