错误1265,使用MYSQL Workbench查询将数据(DATETIME)加载到MySQL中

时间:2018-07-27 09:42:06

标签: mysql sql mysql-workbench

我正在尝试将.txt文件中的一组数据加载到MySQL数据库中。但是,我无法将date_time数据集加载到表中。它返回0000-00-00 00:00:00。谁能告诉我我在做什么错?

我已将以下taxi_movement_data表创建为taxiapp模式。

CREATE TABLE `taxiapp`.`taxi_movement_data` (
`tracked_datetime` DATETIME NOT NULL,
`longitude` DOUBLE NOT NULL,
`lattitude` DOUBLE NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), UNIQUE INDEX `id_UNIQUE` (`id` ASC));

我的.txt文件示例如下。

2018-06-01T23:51:09+08:00,103.62926,1.30081
2018-06-01T23:51:09+08:00,103.63598,1.27931
2018-06-01T23:51:09+08:00,103.6375,1.34143

我的SQL查询如下

LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/uploads/ltasampledata.txt' 
INTO TABLE taxi_movement_data 
FIELDS TERMINATED BY ',' enclosed by '"'
LINES TERMINATED BY '\r\n'
SET tracked_datetime = DATE_ADD(DATE_FORMAT(substring(@tracked_datetime,1,19), '%Y-%m-%d %H:%i:%s'),INTERVAL 8 HOUR);

结果如下,无法加载日期时间。 “ tracked_datetime”返回0000-00-00 00:00:00。经度,纬度和id运行正常,并且应该正常工作。

Table results after Query.

对不起,我尚未被授权直接在Stackoverflow上加载图像,因此它已成为链接。

错误消息如下

3 row(s) affected, 9 warning(s): 1265 Data truncated for column 'tracked_datetime' at row 1 1261 Row 1 doesn't contain data for all columns 1048 Column 'tracked_datetime' cannot be null 1265 Data truncated for column 'tracked_datetime' at row 2 1261 Row 2 doesn't contain data for all columns 1048 Column 'tracked_datetime' cannot be null 1265 Data truncated for column 'tracked_datetime' at row 3 1261 Row 3 doesn't contain data for all columns 1048 Column 'tracked_datetime' cannot be null Records: 3  Deleted: 0  Skipped: 0  Warnings: 9 0.078 sec

1 个答案:

答案 0 :(得分:0)

我犯了一个愚蠢的错误。无论如何,我通过以下方式对其进行了纠正。希望它可以帮助某人。

在设置tracked_datetime之前添加(@tracked_datetime,经度,纬度)

SQL查询应如下所示。

LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/uploads/ltasampledata.txt' 
INTO TABLE taxi_movement_data 
FIELDS TERMINATED BY ',' enclosed by '"'
LINES TERMINATED BY '\r\n'
(@tracked_datetime,longitude,lattitude)
SET tracked_datetime = DATE_ADD(DATE_FORMAT(substring(@tracked_datetime,1,19), '%Y-%m-%d %H:%i:%s'),INTERVAL 8 HOUR);