我有一张这样的桌子:
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
我该如何解决?
答案 0 :(得分:0)
我认为您的源数据具有这些\r
控制字符,因为LOAD DATA
通常不会将数据添加到源文件中(除非您告知这样做,否则不会出现)就是这种情况)。我们可以尝试在RTRIM
列上运行AuditStatus
:
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);
答案 1 :(得分:0)
正如@Sloan所建议的,我更改了行终止符,从而解决了问题。 这是最终代码。
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\r\n\'
IGNORE 1 LINES