MySQL导入utf8编码的CSV文件

首先,作为测试,我们在这里创建一个名为testdb的数据库,和一个名为test_table的表:

create database if not exists testdb default charset utf8 collate utf8_general_ci;
use testdb;
drop table if exists test_table;
create table test_table (
    id integer,
    name varchar(30),
    age integer,
    point decimal(6,2),
    brief varchar(30)
);

然后我们创建一个CSV文件test.csv,设置文件的编码为utf8,编辑内容如下:

1,"刘德华",23,96.12,"我爱你亲爱的姑娘"
2,"周杰伦",22,93.2,"七里香"
3,,,,
4,"周润发",,,"有没有人曾告诉你"

然后进入mysql命令行(或使用前端工具),执行如下SQL:

load data local infile 'd:\test.csv' 
into table testdb.test_table character set utf8
fields terminated by ',' optionally enclosed by '"' escaped by '"' 
lines terminated by '
'

我们可以通过如下SQL查询结果:

select * from testdb.test_table

得到的结果如下:

id name age point brief
1 刘德华 23 96.12 我爱你亲爱的姑娘
2 周杰伦 22 93.20 七里香
3 0 0.00
4 周润发 0 0.00 "有没有人曾告诉你"

自此,四条数据成功插入数据库中。

原文地址:https://www.cnblogs.com/zifeiy/p/9982899.html