MySQL: LOAD DATA LOCAL INFILE 添加额外字符 '\r'
MySQL: LOAD DATA LOCAL INFILE adds extra character '\r'
我有一个 table 这样的:
CREATE TABLE `tblinquiries` (
`UID` varchar(50) DEFAULT NULL,
`ReviewDate` date NOT NULL,
`InquiryId` varchar(50) DEFAULT NULL,
`AuditStatus` varchar(50) DEFAULT NULL,
PRIMARY KEY (`InquiryId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我有一个包含数据的 csv 文件:
UID,ReviewDate,InquiryId,AuditStatus
UID1,2018-07-06,109814969,Check
UID2,2018-07-06,109866072,Check
UID3,2018-07-06,109911408,Check
UID4,2018-07-06,109798278,Check
我使用下面的命令来上传数据:
$location = '../uploads/';
$name = $_FILES["file"]["name"];
$filePath = $location.$name;
$table = 'tblinquiries';
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
它上传数据但从第二行添加额外的字符“\r”。我导出数据并得到如下结果:
('UID4', '2018-07-06', '109798278', 'Check'),
('UID1', '2018-07-06', '109814969', 'Check\r'),
('UID2', '2018-07-06', '109866072', 'Check\r'),
('UID3', '2018-07-06', '109911408', 'Check\r');
运行之后:
SELECT AuditStatus, LENGTH(AuditStatus) FROM `tblinquiries`
得到:
AuditStatus LENGTH(AuditStatus)
Check 5
Check 6
Check 6
Check 6
我该如何解决这个问题?
我假设您的源数据具有那些 \r
控制字符,因为 LOAD DATA
通常不会将数据添加到源文件(除非您告诉它这样做,但它不会似乎是这样)。我们可以在 AuditStatus
列上尝试 运行 RTRIM
:
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
(UID, ReviewDate, InquiryId, @AuditStatus)
SET AuditStatus = RTRIM(@AuditStatus);
正如@Sloan 所建议的,我更改了行终止符并解决了问题。
这是最终代码。
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\r\n\'
IGNORE 1 LINES
我有一个 table 这样的:
CREATE TABLE `tblinquiries` (
`UID` varchar(50) DEFAULT NULL,
`ReviewDate` date NOT NULL,
`InquiryId` varchar(50) DEFAULT NULL,
`AuditStatus` varchar(50) DEFAULT NULL,
PRIMARY KEY (`InquiryId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我有一个包含数据的 csv 文件:
UID,ReviewDate,InquiryId,AuditStatus
UID1,2018-07-06,109814969,Check
UID2,2018-07-06,109866072,Check
UID3,2018-07-06,109911408,Check
UID4,2018-07-06,109798278,Check
我使用下面的命令来上传数据:
$location = '../uploads/';
$name = $_FILES["file"]["name"];
$filePath = $location.$name;
$table = 'tblinquiries';
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
它上传数据但从第二行添加额外的字符“\r”。我导出数据并得到如下结果:
('UID4', '2018-07-06', '109798278', 'Check'),
('UID1', '2018-07-06', '109814969', 'Check\r'),
('UID2', '2018-07-06', '109866072', 'Check\r'),
('UID3', '2018-07-06', '109911408', 'Check\r');
运行之后:
SELECT AuditStatus, LENGTH(AuditStatus) FROM `tblinquiries`
得到:
AuditStatus LENGTH(AuditStatus)
Check 5
Check 6
Check 6
Check 6
我该如何解决这个问题?
我假设您的源数据具有那些 \r
控制字符,因为 LOAD DATA
通常不会将数据添加到源文件(除非您告诉它这样做,但它不会似乎是这样)。我们可以在 AuditStatus
列上尝试 运行 RTRIM
:
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
(UID, ReviewDate, InquiryId, @AuditStatus)
SET AuditStatus = RTRIM(@AuditStatus);
正如@Sloan 所建议的,我更改了行终止符并解决了问题。 这是最终代码。
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\r\n\'
IGNORE 1 LINES