mysql控制台导入csv文件

导入csv文件

Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use demo;
Database changed
mysql> select * from test2;
Empty set (0.01 sec)

mysql> create table test3(name varchar(20),course varchar(20),score integer);
Query OK, 0 rows affected (0.03 sec)

mysql> load data infile 'C:/csvToDB/xxx.csv'
    -> into table test3
    -> fields terminated by ',' optionally enclosed by '"' escaped by '"'
    -> lines terminated by '\r\n';
Query OK, 5 rows affected (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from test3;
+--------+--------+-------+
| name   | course | score |
+--------+--------+-------+
| 张三   | 语文   |    23 |
| 张三   | 数学   |    63 |
| 张三   | 英语   |    12 |
| 李四   | 数学   |    34 |
| 李四   | 语文   |    34 |
+--------+--------+-------+
5 rows in set (0.00 sec)

mysql> 

导入csv文件,忽略首行

  

mysql> select * from test3;
+--------+--------+-------+
| name   | course | score |
+--------+--------+-------+
| 张三   | 语文   |    23 |
| 张三   | 数学   |    63 |
| 张三   | 英语   |    12 |
| 李四   | 数学   |    34 |
| 李四   | 语文   |    34 |
| 君九   | 数学   |    99 |
| 秦十   | 语文   |   100 |
+--------+--------+-------+
7 rows in set (0.00 sec)

mysql> load data infile 'C:/csvToDB/aaa.csv'
    -> into table test3
    -> fields terminated by ',' optionally enclosed by '"' escaped by '"'
    -> lines terminated by '\r\n'
    -> ignore 1 rows;
Query OK, 2 rows affected (0.09 sec)
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from test3;
+--------+--------+-------+
| name   | course | score |
+--------+--------+-------+
| 张三   | 语文   |    23 |
| 张三   | 数学   |    63 |
| 张三   | 英语   |    12 |
| 李四   | 数学   |    34 |
| 李四   | 语文   |    34 |
| 君九   | 数学   |    99 |
| 秦十   | 语文   |   100 |
| 十一   | 语文   |    91 |
| 十二   | 数学   |    89 |
+--------+--------+-------+
9 rows in set (0.01 sec)

mysql>

关于csv文件导入数据及乱码的解决
    1.将数据写入一个新建的excel文件中,
     2.数据写入excel文件后,将excel文件另存为xxx.csv(文件)
     3.选择xxx.csv文件选择记事本打开文件,然后另存为xxx.csv,在另存为界面选择编码格式为UTF-8,覆盖并保存文件xxx.csv
     4.再将此时的xxx.csv文件导入数据库就不会乱码
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值