我正在尝试使用LOAD DATA LOCAL INFILE将数据从csv导入MySQL。对于一个名为“count”的整数列,它具有所有空值,我得到的错误是1366个不正确的整数值:
我试过了count = IF(@count='',NULL,@count)
CREATE TABLE MySchema.response:
CREATE TABLE MySchema.response
(
`id` int,
`name` varchar(500),
`count` int,
);
将数据加载到表中:
LOAD DATA LOCAL INFILE 'C:/response.csv'
INTO TABLE MySchema.response
FIELDS TERMINATED BY ',' optionally ENCLOSED by '"'
ignore 1 lines
(id, name, @count)
SET count = IF(@count='',NULL,@count)
示例csv文件数据
1,abc,
2,xyz,
答案 0 :(得分:0)
您忘了定义var,请参阅下面的正确LOAD DATA INFILE ...
声明:
root@localhost [test]> SHOW CREATE TABLE `Response`\G
*************************** 1. row ***************************
Table: Response
Create Table: CREATE TABLE `Response` (
`id` int(11) DEFAULT NULL,
`name` varchar(500) DEFAULT NULL,
`count` int(11) DEFAULT NULL
)
root@localhost [test]> LOAD DATA INFILE '~/Desktop/data.csv' INTO TABLE Response FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (id, name, @count) SET count = IF(@count = '', NULL, @count);
root@localhost [test]> SELECT * FROM `Response`;
+------+------+-------+
| id | name | count |
+------+------+-------+
| 1 | abc | NULL |
| 2 | xyz | 12345 |
+------+------+-------+
档案data.csv
1,abc,
2,xyz,12345
答案 1 :(得分:0)
有点晚了,但它可能会帮助遇到同样问题的人。
您应该检查换行符(MySQL 以某种方式解析第三个文本列)。 还要确保您的 CSV 文件没有 BOM(位序标记)。
LOAD DATA LOCAL INFILE 'd:/response.csv'
INTO TABLE response
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
IGNORE 0 LINES
(id, NAME, @count)
SET COUNT = IF(@count='' OR @count='\r' OR @count='\n' OR @count='\r\n' ,NULL,@count)
<块引用>
1 个查询执行,1 个成功,0 个错误,0 个警告
查询:LOAD DATA LOCAL INFILE 'd:/response.csv' INTO TABLE 响应 FIELDS TERMINATED BY ',' 可选 ENCLOSED by '"' 忽略 1 行...
1 行受影响
执行时间:0.004 秒传输时间:0.006 秒总时间: 0.011 秒
如果您的 CSV 文件中 COUNT 的所有值都是空值,这种更简单的方法将起作用:
LOAD DATA LOCAL INFILE 'd:/response.csv'
INTO TABLE response
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
IGNORE 1 LINES
(id, NAME, @dummy)