[英]Error 1452 MySQL Load Infile (but correct data)
I'm trying to load data from a .txt using LOAD DATA INFILE, the problem is that i get error 1452, but the foreign keys referred are present in my DB. 我正在尝试使用LOAD DATA INFILE从.txt加载数据,问题是出现错误1452,但是所引用的外键存在于数据库中。 Also other LOAD DATA are working, just can't solve this and 1 other load.
另外其他LOAD DATA也在工作,只是不能解决这个和另外1个负载。
I've checked the referred data, and are present in DB before i load. 我已经检查了所引用的数据,并且在加载之前已存在于数据库中。 Also the columns are of the same type.
列也属于同一类型。 I tried reinstalling MySQL still not working (but a friend of mine using the same code/.txt can load the data).
我尝试重新安装MySQL仍然无法正常工作(但是我的一个朋友使用相同的代码/.txt可以加载数据)。 I can load the .txt if it consists of a single line, but when adding a second one i got the error.
如果它由一行组成,我可以加载.txt,但是添加第二行时出现错误。
-- The table referred:
CREATE TABLE Categoria (
Nome VARCHAR(50) NOT NULL,
Immagine MEDIUMBLOB,
PRIMARY KEY (Nome));
-- The table with FK:
CREATE TABLE Sottocategoria_Di (
Categoria1 VARCHAR(50) NOT NULL,
Categoria2 VARCHAR(50) NOT NULL,
PRIMARY KEY (Categoria1, Categoria2),
FOREIGN KEY (Categoria1) REFERENCES Categoria(Nome) ON DELETE NO ACTION,
FOREIGN KEY (Categoria2) REFERENCES Categoria(Nome) ON DELETE CASCADE);
INSERT INTO Categoria VALUES ('Chitarra', NULL);
INSERT INTO Categoria VALUES ('Chitarra Acustica', NULL);
INSERT INTO Categoria VALUES ('Chitarra Classica', NULL);
LOAD DATA INFILE "C:/ProgramData/MySQL/MySQL Server
8.0/Uploads/MusicShop/Sottocategoria.txt" INTO TABLE
Music.Sottocategoria_Di
CHARACTER SET latin1
FIELDS TERMINATED BY '|'
ENCLOSED BY ''
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(Categoria1,Categoria2);
-- Sottocategoria.txt
Categoria1,Categoria2
Chitarra Classica|Chitarra
Chitarra Acustica|Chitarra
A friend of mine reinstalled MySQL and using the same exact script/.txt can load the file but i still can't. 我的一个朋友重新安装了MySQL,并使用相同的确切脚本/.txt可以加载文件,但我仍然无法加载。
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails ( music
. sottocategoria_di
, CONSTRAINT sottocategoria_di_ibfk_2
FOREIGN KEY ( Categoria2
) REFERENCES categoria
( Nome
) ON DELETE CASCADE) 错误代码:1452不能添加或更新子行,外键约束失败(
music
。 sottocategoria_di
,约束sottocategoria_di_ibfk_2
外键( Categoria2
)参考文献categoria
( Nome
)ON DELETE CASCADE)
The problem was using lines terminated by '\\n', switched to '\\r\\n' and it worked! 问题是使用以'\\ n'结尾的行,切换到'\\ r \\ n',它起作用了! Still don't know why it works for other files .txt i'm using (same OS Windows)
仍然不知道为什么它适用于我正在使用的其他.txt文件(与Windows OS相同)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.