批量插入会跳过输入文件中的一个字段

时间:2014-05-06 23:56:57

标签: mysql bulkinsert

在批量插入中工作,跳过2条记录。说明如下:

My table (works fine, Auto-incrementing Job_Id):



create table avjobs ( Job_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, 
Job_Name varchar(255), Job_Seq varchar(255), Job_Date varchar(255), 
Start_Time time, End_Time time, Runtime time, Status varchar(255) );

这是我的csv文件:

JOB1A|0029|20140506|14:01:05|15:00:01|0:59:45|FINISHED
JOB2B|0030|20140506|15:01:05|16:00:01|0:59:55|INITIATED

这是我正在使用的BULK INSERT:

LOAD DATA LOCAL INFILE '/tmp/jobs.csv' INTO TABLE avjobs FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
Query OK, 3 rows affected, 9 warnings (0.00 sec)

以下是select:

的输出
mysql> select * from avjobs;
+--------+----------+----------+----------+------------+----------+----------+--------+
| Job_Id | Job_Name | Job_Seq  | Job_Date | Start_Time | End_Time | Runtime  | Status |
+--------+----------+----------+----------+------------+----------+----------+--------+
|      1 | 0029     | 20140506 | 14:01:05 | 15:00:01   | 00:23:55 | 00:00:00 | NULL   |
|      2 | 0030     | 20140506 | 15:01:05 | 16:00:01   | 00:59:55 | 00:00:00 | NULL   |
+--------+----------+----------+----------+------------+----------+----------+--------+

批量插入会以某种方式跳过作业名称和状态。

你能告诉我语法有什么问题吗?

1 个答案:

答案 0 :(得分:1)

您必须指定列:

LOAD DATA LOCAL INFILE '/tmp/jobs.csv' 
INTO TABLE avjobs (Job_Name, Job_Seq, Job_Date, Start_Time, End_Time, Runtime, Status)    
FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';

因为您的导入文件不包含您的Job_ID,请参阅manual

  

LOAD DATA INFILE'persondata.txt'INTO TABLE persondata;默认情况下,   当LOAD DATA INFILE结尾没有提供列表时   声明,输入行应包含每个表的字段   柱。如果只想加载某些表的列,请指定一个   栏目列表: