我正在尝试使用以下代码将.csv文件加载到mysql数据库中:
LOAD DATA LOCAL INFILE 'c:/mytestfile.csv' INTO TABLE report.test
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(ONE, TWO)
.csv文件如下所示:
ONE,TWO
1,2
report.test中的两列都是整数。
当我运行SQL代码时,我得到report.test的第一行中的两个值都是0。当我在我的实际.csv文件上运行它时,只读取了一小部分行,并且它们由0,NULL和空白填充。
我错过了某个地方的设置吗?
更新1 - 我没有足够的声誉来发布图片,所以我不得不做链接。
更新2
当我使用时:
LINES TERMINATED BY '\r\n'
警告是:
'Warning','1366','Incorrect integer value: \'??\' for column \'ONE\' at row 1'
'Warning','1366','Incorrect integer value: \'??\' for column \'TWO\' at row 1'
'Warning','1262','Row 1 was truncated; it contained more data than there were input columns'
当我使用时:
LINES TERMINATED BY '\n'
警告是:
'Warning','1366','Incorrect integer value: \'??\' for column \'ONE\' at row 1'
'Warning','1366','Incorrect integer value: \'??\' for column \'TWO\' at row 1'
'Warning','1262','Row 1 was truncated; it contained more data than there were input columns'
'Warning','1366','Incorrect integer value: \'?\' for column \'ONE\' at row 2'
'Warning','1261','Row 2 doesn\'t contain data for all columns'
答案 0 :(得分:0)
SQL语句看起来很好。
它在第一行中添加0,因为您也添加了列标题。要忽略第一行,请使用IGNORE 1 LINES
。
我能想到你遇到问题的唯一原因是因为文件没有回车。
请使用以下声明进行测试:
LOAD DATA LOCAL INFILE 'c:/mytestfile.csv' INTO TABLE report.test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ONE, TWO)