MySQL导入utf8编码的CSV文件

发布时间:2020-12-17 09:59:30 作者:互联分享 阅读量:86

首先,作为测试,我们在这里创建一个名为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 '\r\n'

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

select * from testdb.test_table

得到的结果如下:

idnameagepointbrief
1刘德华2396.12我爱你亲爱的姑娘
2周杰伦2293.20七里香
300.00
4周润发00.00"有没有人曾告诉你"

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

www.nqsu.com
桂ICP备15001426号-2
桂公网安备45010002450324