编辑: 我只能使用这种格式存储一行文件的记录。
0,2016-03-31-13:48:16.435,0.705486,1,F2221-222
但是当我尝试存储包含2行或更多行的文件时,我收到错误。
0,2016-03-31-13:48:16.435,0.705486,1,F2221-222
1,2016-03-31-13:48:16.435,0.56606096,2,F2221-222
我正在使用:
load data infile 'flight3021'
into table signal_times
fields terminated BY ','
(signaltime_id,@var1,value,signal_id,flight_number)
SET time = STR_TO_DATE(@var1,'%Y-%m-%d-%H:%i:%S.%f')
错误:
Cannot add or update a child row: a foreign key constraint fails (`cmsdbdev`.`signal_times`, CONSTRAINT `FK_r2e4qo5suuwrlkr08ryebv14i` FOREIGN KEY (`flight_number`) REFERENCES `flight_data` (`flight_number`))
奇怪的是,如果我正常插入这2行就行了,所以它与FK约束违规错误有关。我认为它必须是负载数据infile的一个问题。
感谢。
答案 0 :(得分:1)
'2016-03-31-13:16:24.861'
被加载为“'2016-03-31-13:16:24.861'
”,这就是为什么它不是有效的datetime
。
我使用TRIM
函数来尝试解决这个问题。
对于多行,需要lines terminated by
。
load data infile '/home/jinggang.sjg/data1'
into table signal_times
fields terminated BY ',' ESCAPED BY ""
lines terminated by '\r\n'
(signaltime_id,@var1,value,signal_id,flight_number)
SET time = STR_TO_DATE(@var1,'%Y-%m-%d-%H:%i:%S.%f');
答案 1 :(得分:1)
尝试:
档案:'/path/to/file/flight3021'
0,2016-03-31-13:48:16.435,0.705486,1,F2221-222
1,2016-03-31-13:48:16.435,0.56606096,2,F2221-222
MySQL命令行:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.11 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `signal_times`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `signal_times` (
-> `signaltime_id` INT UNSIGNED,
-> `time` TIMESTAMP(3),
-> `value` DECIMAL(10,8),
-> `signal_id` INT UNSIGNED,
-> `flight_number` VARCHAR(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> LOAD DATA INFILE '/path/to/file/flight3021'
-> INTO TABLE `signal_times`
-> FIELDS TERMINATED BY ','
-> LINES TERMINATED BY '\r\n'
-> (`signaltime_id`,
-> @`var1`,
-> `value`,
-> `signal_id`,
-> `flight_number`
-> )
-> SET `time` = STR_TO_DATE(@`var1`,'%Y-%m-%d-%H:%i:%S.%f');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> SELECT
-> `signaltime_id`,
-> `time`,
-> `value`,
-> `signal_id`,
-> `flight_number`
-> FROM
-> `signal_times`;
+---------------+-------------------------+------------+-----------+---------------+
| signaltime_id | time | value | signal_id | flight_number |
+---------------+-------------------------+------------+-----------+---------------+
| 0 | 2016-03-31 13:48:16.435 | 0.70548600 | 1 | F2221-222 |
| 1 | 2016-03-31 13:48:16.435 | 0.56606096 | 2 | F2221-222 |
+---------------+-------------------------+------------+-----------+---------------+
2 rows in set (0.00 sec)