问题:如何将csv格式的文件导入到MySQL数据库中(csv文件中包含有中文内容)?
CSV文件内容如下:
表结构如下:
----------------------------------------------------------------------------------------------
具体操作如下:
1.修改表及字段的编码方式,使其支持中文汉字
表名称为【demo】,需要修改的字段有username和password
1 ALTER TABLE demo CHARACTER SET gb2312;
2 ALTER TABLE demo MODIFY username varchar(20) CHARACTER SET gb2312;
2 ALTER TABLE demo MODIFY username varchar(20) CHARACTER SET gb2312;
3 ALTER TABLE demo MODIFY password varchar(20) CHARACTER SET gb2312;
2.查看编码修改结果
SHOW CREATE TABLE demo;
3.导入csv文件,文件名称和路径为D:/insert_data.csv
1 LOAD DATA INFILE "d:/insert_data.csv"
2 REPLACE INTO TABLE DEMO
3 CHARACTER SET gb2312
4 FIELDS TERMINATED BY "," ENCLOSED BY ""
5 LINES TERMINATED BY "\r\n";
2 REPLACE INTO TABLE DEMO
3 CHARACTER SET gb2312
4 FIELDS TERMINATED BY "," ENCLOSED BY ""
5 LINES TERMINATED BY "\r\n";
4.执行成功后可以查看导入效果