LOAD DATA INFILE 不会导入 CSV 数据源中的所有行

发布于 2024-11-08 01:17:27 字数 1397 浏览 0 评论 0原文

我正在尝试将 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;

我正在将数据加载到的表的格式如下:

delimiter $ $

CREATE TABLE `hospital_quality_scores` (
  `ProviderNumber` varchar(8) NOT NULL,
  `HospitalName` varchar(50) DEFAULT NULL,
  `State` varchar(2) DEFAULT NULL,
  `MeasureCode` varchar(25) NOT NULL,
  `Question` longtext,
  `AnswerDescription` longtext,
  `AnswerPercent` int(11) DEFAULT NULL,
  `NumberofCompletedSurveys` varchar(50) DEFAULT NULL,
  `SurveyResponseRatePercent` varchar(50) DEFAULT NULL,
  `Footnote` longtext,
  PRIMARY KEY (`ProviderNumber`,`MeasureCode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$

有人知道为什么会发生这种情况吗?似乎只有部分记录实际上被正确插入。

I'm trying to load data from a CSV file into a MySQL database, and noticed that a large number of records seem to be skipped when I import the file.

The data comes from a Government source, and is very oddly formatted with single quotes, etc in unusual places. Here's a sample of a record not getting inserted:

"'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",""

This record, however, does get inserted:

"'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",""

The SQL I'm using to load the data is here:

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;

The format of the table I'm loading the data into is as follows:

delimiter $$

CREATE TABLE `hospital_quality_scores` (
  `ProviderNumber` varchar(8) NOT NULL,
  `HospitalName` varchar(50) DEFAULT NULL,
  `State` varchar(2) DEFAULT NULL,
  `MeasureCode` varchar(25) NOT NULL,
  `Question` longtext,
  `AnswerDescription` longtext,
  `AnswerPercent` int(11) DEFAULT NULL,
  `NumberofCompletedSurveys` varchar(50) DEFAULT NULL,
  `SurveyResponseRatePercent` varchar(50) DEFAULT NULL,
  `Footnote` longtext,
  PRIMARY KEY (`ProviderNumber`,`MeasureCode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$

Anyone have any ideas why this is happening? It seems that only have of the records are actually being inserted correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初雪 2024-11-15 01:17:27

难道是你的主键阻止了附加数据的插入?

查找已插入的 ProviderNumber 为“'050441'”且 MeasureCode 为“H_HSP_RATING_7_8”的记录,如果有其中之一,则为重复键问题。

您可能需要将“AnswerDescription”添加到主键来解决此问题。

问候,

戴夫

Could it be your primary key is preventing the additional data from being inserted?

Look for a record that has been inserted with a ProviderNumber of "'050441'" and a MeasureCode of "H_HSP_RATING_7_8", if you have one of those, then it is a duplicate key problem.

You may need to add "AnswerDescription" to the primary key to get round this issue.

Regards,

Dave

烏雲後面有陽光 2024-11-15 01:17:27

实际上我在想你的问题可能更多地与第一个值被双引号(即它在“'value'”中被引用两次)有关,这可能导致你试图插入的值是'050441' ,而不是应有的 050441 。

无论如何,如果没有特殊处理,您将插入额外的单引号,我认为您可能无意这样做。

祝你好运,愿你的所有代码都能完美运行!

罗德尼

Actually I'm thinking maybe your problem has more to do with the first value being double quoted (i.e. it is quoted twice as in "'value'"), which is probably resulting in the value you are trying to insert being '050441', not 050441 like it should be.

At any rate, without special handling, you are going to be INSERTing the extra single quotes, which I am thinking you probably did not mean to do.

Good Luck and may all your code run flawlessly!

Rodney

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文