如何从mysql中的文件插入到我的表中?

时间:2015-03-28 18:35:00

标签: mysql

我有一个file.dat如下:

"Evan","E","Wallis","222222200","1958-01-16","134 Pelham, Milwaukee, WI","M","92000.00",null,"7"
"Jared","D","James","111111100","1966-10-10","123 Peachtree, Atlanta, GA","M","85000.00",null,"6"

我想将这些值插入到我的表中。

我尝试了以下但我没有成功。

LOAD DATA LOCAL INFILE "C:/file.dat" 
INTO TABLE table
;

没有错误但是有一个警告,表格没有填充数据。

LOAD DATA LOCAL INFILE "C:/file.dat" INTO TABLE table
0 row(s) affected, 64 warning(s):
1265 Data truncated for column 'fname' at row 1
1261 Row 1 doesn't contain data for all columns... 

以下是table的创建方式。

CREATE TABLE table(
  fname    varchar(15) not null, 
  minit    varchar(1),
  lname    varchar(15) not null,
  ssn      char(9),
  bdate    date,
  address  varchar(50),
  sex      char,
  salary   decimal(10,2),
  superssn char(9),
  dno      integer(4),
  primary key (ssn),
  foreign key (superssn) references employee(ssn)
);

1 个答案:

答案 0 :(得分:1)

默认情况下,MySQL正在查找以制表符分隔的文件。请尝试以下方法:

LOAD DATA LOCAL INFILE "C:/file.dat" 
INTO TABLE table
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
;