由于本项目中插入数据表量大而且格式统一,故首先使用创建字段create的方式创建表格,再使用load的方式导入数据字段。
LOAD DATA INFILE 'D:\\electric\\201503.csv'INTO TABLE hjqk_mxb_201503FIELDS 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_201503FIELDS 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_201503character 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);查阅数据时,发现自己插入数据非常不完整,只要是原文