- 使用navicat的客户端插入csv的数据文件,有一种非常简单的方式,即使用导入向导,直接根据数据匹配即可。
由于本项目中插入数据表量大而且格式统一,故首先使用创建字段create的方式创建表格,再使用load的方式导入数据字段。
LOAD DATA INFILE 'D:\\electric\\201503.csv'
INTO TABLE hjqk_mxb_201503
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
lines terminated by '\r\n'
ignore 1 lines
(gzdh,ywly,gdlx,ywlb,ssqj,nzdw,zrbm,fwdz,gzdnr,gdxdsj,gdwbsj,slzxy,yhlx,yhbh,lxr,lxrdh,clcs,hfjg, khpy);
使用上述代码插入数据时,总是出现报错“[Err] 29 - File 'D:\electric\201503.csv' not found (Errcode: 2 - No such file or directory)”,根据http://stackoverflow.com/questions/15166079/mysql-workbench-load-data-local-infile-cannot-find-file-or-directory显示,发现自己编辑的代码缺一个local,故修改代码为:
LOAD DATA LOCAL INFILE 'D:\\electric\\201503.csv'
INTO TABLE hjqk_mxb_201503
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
lines terminated by '\r\n'
ignore 1 lines
(gzdh,ywly,gdlx,ywlb,ssqj,nzdw,zrbm,fwdz,gzdnr,gdxdsj,gdwbsj,slzxy,yhlx,yhbh,lxr,lxrdh,clcs,hfjg, khpy);
使用上述代码插入数据时,导入的csv数据总是乱码,综合查阅资料才发现自己的代码缺少转化为utf-8的代码行,故修改代码为:
LOAD DATA LOCAL INFILE 'D:\\electric\\201503.csv'
INTO TABLE hjqk_mxb_201503
character set utf8
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
lines terminated by '\r\n'
ignore 1 lines
(gzdh,ywly,gdlx,ywlb,ssqj,nzdw,zrbm,fwdz,gzdnr,gdxdsj,gdwbsj,slzxy,yhlx,yhbh,lxr,lxrdh,clcs,hfjg, khpy);
查阅数据时,发现自己插入数据非常不完整,只要是原文本文件出现一点数据问题,使用load的方式便不再插入数据源。根据http://www.jb51.net/article/18122.htm显示,修改代码为:
Load Data LOCAL InFile 'D:\\201503.csv' Into Table hjqk_mxb_201503
character set utf8
Fields Terminated By ',' Enclosed By '"' Escaped By '"' Lines Terminated By '\r\n';
数据导入正确,且并未缺少行列。
|