简体   繁体   English

LOAD DATA INFILE仅加载0,NULL和空白

[英]LOAD DATA INFILE only Loads 0s, NULLs, and Blanks

I am trying to load a .csv file into a mysql database using the following code: 我正在尝试使用以下代码将.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)

The .csv file looks like: .csv文件如下所示:

ONE,TWO
1,2

And the two columns in report.test are both integers. 并且report.test中的两列都是整数。

When I run the SQL code, I get the the both values in the first row of report.test are 0s. 当我运行SQL代码时,在report.test第一行中得到的两个值均为0。 When I run this on my actual .csv file, only a fraction of the rows are read, and they are populated by 0s, NULLs, and Blanks. 当我在实际的.csv文件上运行此命令时,仅读取了一部分行,并用0,NULL和空白填充了行。

Am I missing a setting somewhere? 我在某处缺少设置吗?


UPDATE 1 - I dont have enough reputation to post pictures, so I had to do links. 更新1-我没有足够的声誉来张贴图片,所以我必须做链接。

Table Structure 表结构

Example File 示例文件


UPDATE 2 更新2

When I use: 当我使用时:

LINES TERMINATED BY '\r\n'

The warnings are : 警告是:

'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'

When I use: 当我使用时:

LINES TERMINATED BY '\n'

The warnings are : 警告是:

'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'

The SQL statement looks fine. SQL语句看起来不错。

It adds 0s in the first row because you're adding the column headers too. 它在第一行中添加0,因为您也添加了列标题。 To ignore the first line use IGNORE 1 LINES . 要忽略第一行,请使用IGNORE 1 LINES

The only reason I can think of why you're having problems is because of the file that doesn't have carriage return. 我能想到您为什么会出现问题的唯一原因是因为该文件没有回车符。

Please test with the following statement: 请使用以下语句进行测试:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM