Mysql
導入 MySQL 表時日期時間值不正確
將 .csv 文件導入 MySQL 表時遇到問題。
mysql> LOAD DATA INFILE '/var/lib/mysql-files/script_output.csv' REPLACE INTO TABLE incidents columns terminated by ',' optionally enclosed by '"' ignore 1 lines;
ERROR 1292 (22007): Incorrect datetime value: '2021-08-29T04:18:35Z' for column 'incident_start' at row 1
但是,我可以在沒有命令問題的情況下導入
IGNORE
。mysql> LOAD DATA INFILE '/var/lib/mysql-files/script_output.csv' IGNORE INTO TABLE incidents columns terminated by ',' optionally enclosed by '"' ignore 1 lines;
Query OK, 0 rows affected, 809 warnings (0.01 sec) Records: 266 Deleted: 0 Skipped: 266 Warnings: 809
我的要求是替換/更新列,以防表中先前值的任何更新。
表結構
+----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | incident | varchar(12) | NO | PRI | NULL | | | description | varchar(300) | YES | | NULL | | | status | varchar(12) | YES | | NULL | | | urgency | varchar(7) | YES | | NULL | | | service | varchar(27) | YES | | NULL | | | trigger | varchar(25) | YES | | NULL | | | team | varchar(20) | YES | | NULL | | | incident_start | datetime(6) | YES | | NULL | | | incident_end | datetime(6) | YES | | NULL | | | resolved_by | varchar(20) | YES | | NULL | | +----------------+--------------+------+-----+---------+-------+
在使用替換選項導入 .csv 時,我們如何解決日期時間不正確的問題?
我得到了解決方案。我們只需要在上傳 csv 文件時刪除 STRICT_TRANS_TABLES 模式。我們可以使用以下命令
set session sql_mode = ''; set session sql_mode = 'NO_ENGINE_SUBSTITUTION';
大功告成,我們可以用REPLACE上傳數據了
MariaDB [pagerduty]> LOAD DATA INFILE '/var/lib/mysql/pagerduty/script_output.csv' REPLACE INTO TABLE temp_incidents columns terminated by ',' optionally enclosed by '"' ignore 1 lines; Query OK, 246 rows affected, 504 warnings (0.004 sec) Records: 246 Deleted: 0 Skipped: 0 Warnings: 504
注意:上述SQL模式需要在每次上傳文件到表之前設置。