4

Here is my code.

TRUNCATE TABLE dsw_data.inventory_sss2;
SET AUTOCOMMIT=0; 
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv' INTO TABLE dsw_data.inventory_sss2 FIELDS TERMINATED BY  ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r'; 
COMMIT;

Here is the resulting error

Error Code: 1366. Incorrect integer value: 'CUST NO' for column 'sno' at row 1"

I want the first row of the CSV to be the table headers, and the other rows to be the data.

Also how do I specify which rows should be numeric (If this is even needed, I'm not sure).

I have searched here and google, but haven't seen code samples to set the headers like I want.

4

2 回答 2

4

要回答您的第一个问题,您需要使用以下IGNORE 1 LINES语法:

TRUNCATE TABLE dsw_data.inventory_sss2;
SET AUTOCOMMIT=0; 
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv' 
INTO TABLE dsw_data.inventory_sss2 
FIELDS TERMINATED BY  ',' 
OPTIONALLY ENCLOSED BY '"' 
LINES TERMINATED BY '\r'; 
IGNORE 1 LINES
COMMIT;

更新为显式命名列并将主键设置为 null 以使用自动增量:

TRUNCATE TABLE dsw_data.inventory_sss2;
SET AUTOCOMMIT=0; 
LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv' 
INTO TABLE dsw_data.inventory_sss2 
FIELDS TERMINATED BY  ',' 
OPTIONALLY ENCLOSED BY '"' 
LINES TERMINATED BY '\r'; 
IGNORE 1 LINES
(your_first_column, your_second_column,...)
SET sno = NULL
COMMIT;
于 2012-05-29T16:52:26.337 回答
0

添加

IGNORE 1 LINES

对您的LOAD DATA查询:

LOAD DATA INFILE 'c:/inetpub/wwwroot/Data/inventory_sss.csv' INTO TABLE dsw_data.inventory_sss2 FIELDS TERMINATED BY  ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r' IGNORE 1 LINES;

您不需要指定哪些字段是数字的,它将基于您要加载到的表。

于 2012-05-29T16:52:11.880 回答