MySQL:LOAD DATA INFILE产生不正确的表结果

时间:2018-11-17 04:39:27

标签: mysql sql csv mysql-workbench innodb

我正在尝试使用LOAD DATA INFILE将数据加载到表中。

我的桌子:

Alcohol name varchar(45)
Type varchar(45)
ABV FLOAT
Brewery INT(11)
Average Price DOUBLE
Description VARCHAR(1000)

我的CSV文件如下:

Alcohol name,Type,ABV,Brewery,Average Price,Description
Hocus Pocus,Beer,4.5,812,0,"Our take on a classic summer ale.  A toast to weeds, rays, and summer haze.  A light, crisp ale for mowing lawns, hitting lazy fly balls, and communing with nature, Hocus Pocus is offered up as a summer sacrifice to clodless days.Its malty sweetness finishes tart and crisp and is best apprediated with a wedge of orange."
Grimbergen Blonde,Beer,6.699999809,264,0,None
Widdershins Barleywine,Beer,9.100000381,779,0,None
Lucifer,Beer,8.5,287,0,None
Bitter,Beer,4,1056,0,None
Winter Warmer,Beer,5.199999809,1385,0,None
Winter Welcome 2007-2008,Beer,6,1099,0,None

etc....

我的LOAD命令如下:

LOAD DATA LOCAL INFILE '/home/leo/CS336/Tables/beers.csv' INTO TABLE Alcohol FIELDS TERMINATED BY ',' IGNORE 1 LINES; 

我得到的垃圾表如下:

Alcohol name Type  ABV Brewery Average Price Description
Golden Ale  NULL    0   0   NULL NULL   
* 10.5% (2008)  NULL    0   0   NULL NULL       
* 15.5 gallon keg   NULL    0   0       NULL NULL   
* 22 oz. bottles    NULL    0   0   NULL NULL       
* 5.17 gallon keg   NULL    0   0   NULL NULL       
* 9.78% (2007)"     NULL    0   0   NULL NULL   
* Available March through August    NULL    0   0       NULL NULL   
etc...      

1 个答案:

答案 0 :(得分:1)

我根据您的描述创建了一个表(foreach ($result as $obj) { print_r($obj['album']['images']); echo $obj['album']['images'][0]['url']; } 我添加了下划线)

Average_Price

我这样加载您的示例文件

 create table Alcohol (
   name varchar(45), 
   Type varchar(45), 
   ABV FLOAT, 
   Brewery INT(11), 
   Average_Price DOUBLE, 
   Description VARCHAR(1000)
);

我认为我得到了合理的结果:

 LOAD DATA LOCAL INFILE '/tmp/alcohol.csv' INTO TABLE Alcohol FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' IGNORE 1 LINES; 

`SELECT * from Alcohol;