导入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文件导入数据库就不会乱码