我收到了错误
警告| 1366 |不正确的整数值:''对于第34行的列running_time_minutes'
使用以下脚本。该错误也适用于列footage_lenght_feet。我的.csv对于许多单元格都有空值,但这是因为答案未知。我不明白为什么即使我为这个列指定了'default NULL',它仍然会返回错误。
-- create the Films table
DROP TABLE IF EXISTS Films;
CREATE TABLE Films (
title varchar (100) NOT NULL,
alternate_title varchar (50),
year_of_release varchar (15),
Country varchar (20),
running_time_minutes int (10) default NULL,
footage_lenght_feet int (10) default NULL,
FilmStock varchar (10),
FilmGauge varchar (10),
BW_color varchar (10),
notes varchar (255),
print_publications varchar (255),
existent_print varchar (255),
URL_1 varchar (100),
URL_2 varchar (100),
URL_3 varchar (100),
PRIMARY KEY(title),
FOREIGN KEY(Country) REFERENCES Country (Country),
FOREIGN KEY(FilmStock) REFERENCES FilmStock (FilmStock),
FOREIGN KEY(BW_color) REFERENCES BW_color (BW_color),
FOREIGN KEY(FilmGauge) REFERENCES FilmGauge (FilmGauge));
ENGINE = INNODB;
LOAD DATA LOCAL INFILE "Films.csv"
INTO TABLE Films
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
答案 0 :(得分:2)
您没有发布任何数据,所以这有点推测,但我从您的评论中假设您有一个类似的输入文件:
"Title", "Alternate Title", "Year of Release", "Country","Running Time (minutes)", "Footage Length (feet)", "Filmstock","FilmGauge", "BW/Colour", "Notes", "Print Publications", "Existent Print", "URL1", "URL2","URL3"
"A title","","1958","UK","102","38000","","Kodak","70mm","BW","","","","",""
"Another title","","1958","UK",,"38000","","Kodak","70mm","BW","","","","",""
(请注意,在第二行数据中,与running_time_minutes
对应的字段为空。)
当MySQL读取此文件时,它会根据您提供的说明将每行解析为字段:
LOAD DATA LOCAL INFILE "Films.csv"
INTO TABLE Films
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
然后它尝试将数据强制转换为表中目标列的正确形式。 varchar
列不需要强制,因此它们很容易。在存储数字列之前,需要将数字列强制转换为数字。
这适用于具有数字数据的列(例如字符串102
),但是当MySQL遇到空列时,它会将其视为空字符串。这与NULL
不同,并且不是数字,也不能强制转换为目标列的正确格式。因此,您会看到警告。
你需要在这里给MySQL一些帮助。添加列列表,将用户变量替换为您遇到问题的数字列。然后,您可以给MySQL明确指示如何处理数字字段。
LOAD DATA LOCAL INFILE "Films.csv"
INTO TABLE Films
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
-- Put the numeric fields in user variables (names prefixed by @)
(title, alternate_title, year_of_release, Country, @runningTime,
@footageLength, FilmStock, Filmgauge, BW_color, notes,
print_publications, existent_print, URL_1, URL_2, URL_3
)
-- Now check for empty strings and set the numeric columns to null if they're found
set running_time_minutes = if(@runningTime="", null, @runningTime),
footage_lenght_feet = if(@footageLength="", null, @footageLength)