gpt4 book ai didi

MySQL:LOAD DATA LOCAL INFILE 添加额外字符 '\r'

转载 作者:行者123 更新时间:2023-11-30 21:41:22 24 4
gpt4 key购买 nike

我有一个这样的表:

    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);

关于MySQL:LOAD DATA LOCAL INFILE 添加额外字符 '\r',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51360863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com