我创建了下表:
CREATE TABLE Movie
(Movie_ID INT NOT NULL AUTO_INCREMENT,
Movie_Image BLOB NOT NULL,
Movie_Name VARCHAR(30) UNIQUE NOT NULL,
Movie_Description TEXT,
Movie_Genre VARCHAR(20) UNIQUE NOT NULL,
Movie_Rating VARCHAR(10) UNIQUE NOT NULL,
Movie_Duration INT NOT NULL,
PRIMARY KEY (Movie_ID)
)
然后我尝试插入以下值:
INSERT INTO Movie (Movie_Image, Movie_Name, Movie_Description, Movie_Genre, Movie_Rating, Movie_Duration)
VALUES (LOAD_FILE('E:/justice league.jpg'),'Justice League','Fueled by his restored faith in humanity and inspired by Supermans selfless act, Bruce Wayne enlists newfound ally Diana Prince to face an even greater threat. Together, Batman and Wonder Woman work quickly to recruit a team to stand against this newly awakened enemy. Despite the formation of an unprecedented league of heroes','Action','PG-13',120);
INSERT INTO Movie (Movie_Image, Movie_Name, Movie_Description, Movie_Genre, Movie_Rating, Movie_Duration)
VALUES (LOAD_FILE('E:/thor.jpg'),'Thor: Ragnarok','Imprisoned on the other side of the universe, the mighty Thor finds himself in a deadly gladiatorial contest that pits him against the Hulk, his former ally and fellow Avenger. Thors quest for survival leads him in a race against time to prevent the all-powerful Hela from destroying his home world and the Asgardian civilization.','Action','PG-13',130);
INSERT INTO Movie (Movie_Image, Movie_Name, Movie_Description, Movie_Genre, Movie_Rating, Movie_Duration)
VALUES (LOAD_FILE('E:/star wars.jpg'),'Star Wars: The Last Jedi','Luke Skywalkers peaceful and solitary existence gets upended when he encounters Rey, a young woman who shows strong signs of the Force. Her desire to learn the ways of the Jedi forces Luke to make a decision that changes their lives forever.','Action','PG-13',152);
但是我收到以下错误:
#1048 - Column 'Movie_Image' cannot be null
如何通过MYSQL中的INSERT命令将图像插入表中? 如果提供解决方案代码,那将非常有用。
答案 0 :(得分:0)
创建表时,将BLOB列的大小声明为:
CREATE TABLE test ( ... Movie_Image BLOB, INDEX(Movie_Image(100)), ...);
然后,你的插入:
INSERT INTO Movie (Movie_Image, Movie_Name, Movie_Description, Movie_Genre, Movie_Rating, Movie_Duration)
VALUES (LOAD_FILE('E:/justice league.jpg'),'Justice League','Fueled by his restored faith in humanity and inspired by Supermans selfless act, Bruce Wayne enlists newfound ally Diana Prince to face an even greater threat. Together, Batman and Wonder Woman work quickly to recruit a team to stand against this newly awakened enemy. Despite the formation of an unprecedented league of heroes','Action','PG-13',120),
(LOAD_FILE('E:/thor.jpg'),'Thor: Ragnarok','Imprisoned on the other side of the universe, the mighty Thor finds himself in a deadly gladiatorial contest that pits him against the Hulk, his former ally and fellow Avenger. Thors quest for survival leads him in a race against time to prevent the all-powerful Hela from destroying his home world and the Asgardian civilization.','Action','PG-13',130),
(LOAD_FILE('E:/star wars.jpg'),'Star Wars: The Last Jedi','Luke Skywalkers peaceful and solitary existence gets upended when he encounters Rey, a young woman who shows strong signs of the Force. Her desire to learn the ways of the Jedi forces Luke to make a decision that changes their lives forever.','Action','PG-13',152);
您可以调用单个INSERT INTO(列)VALUES语句,然后在括号()之间用逗号分隔每个值。
答案 1 :(得分:0)
加载文件
读取文件并以字符串形式返回文件内容。要使用此功能,文件必须位于服务器主机上,您必须指定文件的完整路径名,并且您必须具有FILE权限。该文件必须可由所有人读取,其大小必须小于max_allowed_packet字节。如果secure_file_priv系统变量设置为非空目录名,则要加载的文件必须位于该目录中。
如果由于不满足上述条件之一而导致文件不存在或无法读取,则该函数返回NULL。