您好,欢迎访问一九零五行业门户网

mysql中的数据导入与导出

qq群288567073,无商业广告,每日干货电子书+视频分享 荔枝fm手机客户端搜索“挨踢脱口秀”即可订阅我们 视频汇总首页: http://edu.51cto.com/lecturer/index/user_id-4626073.html 为了普及mysql的基本知识,特意弄了这个章节,主要是发现第一次接触的人都
qq群288567073,无商业广告,每日干货电子书+视频分享
荔枝fm手机客户端搜索“挨踢脱口秀”即可订阅我们
视频汇总首页:http://edu.51cto.com/lecturer/index/user_id-4626073.html
为了普及mysql的基本知识,特意弄了这个章节,主要是发现第一次接触的人都不知道怎么弄,或者看不懂,所以这里就详细说下吧
============================================================
数据导入
1.mysqlimport命令行导入数据
在使用mysqlimport命令导入数据时,数据来源文件名要和目标表一致,不想改文件名的话,可以复制一份创建临时文件,示例如下。
建立一个文本users.txt,内容如下:
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/121224f33-0.jpg title=1.png alt=wkiom1vg8vvyjsnwaab6yyoyaiw669.jpg>
创建一个表users
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/1212243s6-1.jpg title=2.png alt=wkiol1vg8q_hcdpfaacuwsehht4443.jpg>
使用mysqlimport将users.txt中数据导入users表
ps f:\> mysqlimport -u root -p123456 zz --default-character-set=gbk --fields-terminated-by=',' f:\users.txtzz.users: records: 3  deleted: 0  skipped: 0  warnings: 0-----------------------------验证----------------------------------mysql> select * from users\g*************************** 1. row ***************************   id: 1003 name: 王五email: wangwu@163.com*************************** 2. row ***************************   id: 1001 name: 张三email: zhangsan@163.com*************************** 3. row ***************************   id: 1002 name: 李四email: lisi@hotmail.com
分列,使用--fields-terninated-by参数来指定每列的分隔符,例如:
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/1212244251-2.jpg title=4.png alt=wkiom1vg8zexztkoaafbhq7oyug243.jpg>
如果列值中出现了分隔符,例如 1004#李#白#libai@hotmail.com
ps f:\> mysqlimport -u root -p7758520 zz  --fields-terminated-by='#' --fields-enclosed-by=\  f:\users.txt
如果遇到一条记录有多行,则可以使用--lines-terminated-by=name来指定行的结束符
ps f:\> mysqlimport -u root -p7758520 zz  --fields-terminated-by='#' --fields-enclosed-by=\  --lines-terminated-by='xxx\n' f:\users.txt
2.使用load data语句导入数据
load data 语句的使用语法如下:
load data [low_priority | concurrent] [local] infile 'file_name'    [replace | ignore]    into table tbl_name    [character set charset_name]    [{fields | columns}        [terminated by 'string']        [[optionally] enclosed by 'char']        [escaped by 'char']    ]    [lines        [starting by 'string']        [terminated by 'string']    ]    [ignore number {lines | rows}]    [(col_name_or_user_var,...)]    [set col_name = expr,...]
刚开始看到这个语法吓了一跳,这么长,其实没这么复杂,一般只需记住load data infile file_name into table tb_name这个即可,示例:
首先创建一个表sql_users,利用上面的users表复制一下
mysql> create table sql_users as select * from users;query ok, 1 row affected (0.06 sec)records: 1  duplicates: 0  warnings: 0mysql> truncate table sql_users;query ok, 0 rows affected (0.00 sec)mysql> select * from sql_users;empty set (0.00 sec)
文本sql_users.txt
1004#李白#libai@hotmail.com1005#杜牧#dumu@hotmail.com1006#杜甫#dufu@hotmail.com1007#苏轼#sushi@hotmail.com
利用load data infilee语句导入数据
mysql> load data infile 'f:\sql_users.txt' into table sql_users fields terminated by '#';query ok, 4 rows affected (0.00 sec)records: 4  deleted: 0  skipped: 0  warnings: 0mysql> select * from sql_users;+------+------+--------------------+| id   | name | email              |+------+------+--------------------+| 1004 | 李白 | libai@hotmail.com| 1005 | 杜牧 | dumu@hotmail.com| 1006 | 杜甫 | dufu@hotmail.com| 1007 | 苏轼 | sushi@hotmail.com  |+------+------+--------------------+4 rows in set (0.00 sec)
如果在导入数据时,遇到字符串无法识别时,一般都是字符集有问题,使用charset选项即可解决
mysql> load data infile 'f:\sql_users.txt' into table sql_users  fields terminated by '#';error 1366 (hy000): incorrect string value: '\xc0\xee\xb0\xd7' for column 'name' at row 1--------------------------------字符集不一样-----------------------mysql> load data infile 'f:\sql_users.txt' into table sql_users  character set gbk fields terminated by '#';query ok, 4 rows affected (0.03 sec)records: 4  deleted: 0  skipped: 0  warnings: 0
load data infile命令默认要导入数据存放在服务上,如果要导入客户端的数据,可以指定local,那么mysql将从客户端读取数据,这样的方式会比服务器上操作要慢一点,因为客户端的数据需要通过网络传输到服务器。
mysql> load data local infile 'f:\sql_users.txt' into table sql_users  fields terminated by '#';
如果需要忽略与主键值重复的记录值或者替换重复值,可以使用ignore或replace选项,但是load data infile命令语法中有两处ignore关键字,前面一个是用来此功能的,后面一个用来指定需要忽略的前n条记录。
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/1212244p3-3.jpg title=5.png alt=wkiom1vg8e3jvnrvaainfpim2hc600.jpg>
如果不想导入数据文件的前n行,使用ignore n lines来处理
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/12122441v-4.jpg title=6.png alt=wkiol1vg84cjinb_aagoj2zgnhe857.jpg>
如果在数据文件中记录行头有某些字符,又不想被导入,可以使用lines starting by来解决,但是如果某行记录不包含这些字符的话,那么这行记录也会被忽略。
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/12122452i-5.jpg title=7.png alt=wkiom1vg8jlaehrbaajo-qukbb0151.jpg>
数据文件为excel文件的处理,首先将excel文件保存为csv格式,这样字段间都是用逗号隔开的,再进行处理。
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/12122464c-6.jpg title=8.png alt=wkiol1vg87_heyfxaajh9oje6ec208.jpg>
数据文件列值中有特殊符号,使用enclosed by来处理。例如,列值中有分隔符
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/1212246124-7.jpg title=8.png alt=wkiol1vg893ya5ifaai8hvppk3e453.jpg>
数据导入时换行符的问题,在上面的示例中,有几个数据导入到表中后,查询时结果显示有点别扭,不知大家注意到了没。
在windows系统中,文本格式的换行符有\r+\n组成,而在linux系统中,换行符是\n。因此出出现上述问题,解决方法就是指定换行符lines terminated by。
mysql> load data infile 'f:\stu.csv' into table stu character set gbk fields terminated by ',' enclosed by '' lines terminated by '\r\n' ignore 1 lines;
表的列数多余数据文件中的列数,解决方法就是指定要导入到表的字段,如下所示
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/1212244314-8.jpg title=8.png alt=wkiom1vg8ppbmfy6aakwc3y6i-e654.jpg>
如果是表的列数少于数据文件中的列数呢,解决办法可以指定用户变量来接收多余的列值,如下
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/12122440e-9.jpg title=8.png alt=wkiol1vg9ckbg5rvaai02wzwtwa553.jpg>
如果表的列数与数据文件的不同,且某些字段类型都不一致,那怎么解决呢?方法如下:
------------------文本----------------------
ps f:\> more .\stu.csv
学号,姓名,班级
4010404,祝小贤,a1012,20,male,信息学院
4010405,肖小杰,a1013,22,female,外院
4010406,钟小喜,a1014,24,male,会计学院
4010407,钟小惠,a1015,26,female,商学院
--------------------处理-------------------------
mysql> desc stu;   //表结构
+--------+-------------+------+-----+---------+-------+
| field  | type        | null | key | default | extra |
+--------+-------------+------+-----+---------+-------+
| sno    | int(11)     | no   | pri | null    |       |
| sname  | varchar(30) | yes  |     | null    |       |
| class  | varchar(20) | yes  |     | null    |       |
| age    | int(11)     | yes  |     | null    |       |
| gender | tinyint(4)  | yes  |     | null    |       |
+--------+-------------+------+-----+---------+-------+
rows in set (0.01 sec)
mysql> load data infile 'f:\stu.csv' into table stu character set gbk fields terminated by ',' enclosed by '' lines ter
minated by '\r\n' ignore 1 lines (sno,sname,class,age,@gender,@x) set gender=if(@gender='male',1,0);
query ok, 4 rows affected (0.09 sec)
records: 4  deleted: 0  skipped: 0  warnings: 0
mysql> select * from stu;
+---------+--------+-------+------+--------+
| sno     | sname  | class | age  | gender |
+---------+--------+-------+------+--------+
| 4010404 | 祝小贤 | a1012 |   20 |      1 |
| 4010405 | 肖小杰 | a1013 |   22 |      0 |
| 4010406 | 钟小喜 | a1014 |   24 |      1 |
| 4010407 | 钟小惠 | a1015 |   26 |      0 |
+---------+--------+-------+------+--------+
rows in set (0.00 sec)
数据导出
数据导出比较简单,只要会select ...into outfile语句即可,例如
mysql> select * from stu into outfile f:\stu_bak.txt  character set gbk fields terminated by '##' lines terminated by'\r\n';
query ok, 4 rows affected (0.00 sec)
-------------------------------stu_bak.txt-----------------------
ps f:\> more .\stu_bak.txt
4010404##祝小贤##a1012##20##1
4010405##肖小杰##a1013##22##0
4010406##钟小喜##a1014##24##1
4010407##钟小惠##a1015##26##0
还有一个select...into dumpfile,这个语句也是将数据导出到文件,但是不能格式化语句,如fields,lines这些,它是将数据原汁原味输出到文件。但是只能输出一个记录,用处不大。
650) this.width=650; src=http://www.68idc.cn/help/uploads/allimg/151111/1212244w6-10.jpg title=qq群二维码.png alt=wkiol1y6nrbrftzqaadqcjpg4hg407.jpg>
其它类似信息

推荐信息