我有一个包含行的 CSV 文件,如下所示:
2,1,abc123,1,2,"Hello World"
2,1,abc123,1,2,"Hello World2"
2,1,abc123,1,2,"Hello World3"
我正在运行以下查询:
LOAD DATA LOCAL INFILE :path INTO TABLE errors
CHARACTER SET 'utf8'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
(import_id, type, code, row, cell, message);
它不会将我的任何行插入数据库。这是错误表的结构:
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| import_id | int(10) unsigned | NO | MUL | NULL | |
| type | int(10) unsigned | NO | | NULL | |
| code | varchar(128) | YES | | NULL | |
| row | int(10) unsigned | YES | | NULL | |
| cell | varchar(32) | YES | | NULL | |
| message | varchar(128) | YES | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
我注意到,如果我更改列的顺序,它会起作用。
例如,在我的 CSV 文件中
1,abc123,1,2,"Hello World",2
1,abc123,1,2,"Hello World2",2
1,abc123,1,2,"Hello World3",2
还更改了查询:
LOAD DATA LOCAL INFILE :path INTO TABLE errors
CHARACTER SET 'utf8'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
(type, code, row, cell, message, import_id);
为什么它使用不同的列顺序?