我正在尝试将CSV文件中的数据加载到MySQL数据库中,并注意到导入文件时似乎跳过了大量记录。
这些数据来自政府来源,并且在不寻常的地方使用单引号等格式非常奇怪。以下是未插入记录的示例:
"'050441'","STANFORD HOSPITAL","CA","H_HSP_RATING_7_8","How do patients rate the hospital overall?","Patients who gave a rating of'7' or '8' (medium)","22","300 or more","37",""
然而,此记录确实已插入:
"'050441'","STANFORD HOSPITAL","CA","H_HSP_RATING_0_6","How do patients rate the hospital overall?","Patients who gave a rating of '6' or lower (low)","8","300 or more","37",""
我用来加载数据的SQL在这里:
mysql> load data infile "c:\\HQI_HOSP_HCAHPS_MSR.csv" into table hospital_qualit
y_scores fields terminated by "," enclosed by '"' lines terminated by "\n" IGNOR
E 1 LINES;
任何人都有任何想法为什么会这样?似乎只有正确插入的记录。
答案 0 :(得分:3)
执行此处提到的失败记录的查询后。
mysql> load data ....;
执行foll代码
mysql> show warnings;
它会显示查询失败的错误吗?并继续下一步解决它。
我用你的数据进行了一次小测试,效果很好:
CREATE TABLE `test2` (
`id` int(11) NOT NULL,
`no` varchar(20) NOT NULL,
`name` varchar(20) NOT NULL,
`country` varchar(20) NOT NULL,
`rating` varchar(20) NOT NULL,
`rate_desc1` varchar(100) NOT NULL,
`rate_desc2` varchar(100) NOT NULL,
`no1` int(11) NOT NULL,
`desc3` varchar(20) NOT NULL,
`no2` int(11) NOT NULL,
`desc4` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
/var/lib/mysql/test/test.csv同时处理n个非工作记录:
1,“'050441'”,“STANFORD HOSPITAL”,“CA”,“H_HSP_RATING_7_8”,“患者如何评价整体医院?”,“评分为'7'或'8'的患者(中)“,”22“,”300或更多“,”37“,”“ 2,“'050441'”,“STANFORD HOSPITAL”,“CA”,“H_HSP_RATING_0_6”,“患者如何评价整体医院?”,“评分为'6'或更低(低)的患者”,“ 8“,”300或更多“,”37“,”“
mysql> load data infile "test.csv" into table test2 fields terminated by "," enclosed by '"' lines terminated by "\n" IGNORE 1 LINES;
<强>结果:强>
查询OK,2行受影响(0.05秒)
记录:2删除:0跳过:0警告:0
结论:所以,我认为不同之处仅在于列的数据类型n请参阅警告genereated