“FIELDS TERMINATED BY”和“LINES TEDINATED BY”有什么问题?

时间:2017-07-10 09:33:55

标签: mysql sql database csv

所以我创建了一个名为specs的mysql表,我想在这个表中导入一个csv文件。

CREATE TABLE specs (
  `Id` INT NOT NULL,
  `Brand` VARCHAR(40) NOT NULL,
  `Horsepower` INT NOT NULL,
  `Range` INT NOT NULL,
  PRIMARY KEY (Id)
);

csv数据如下所示

ID\Brand\Horsepower\Range
1\Mercedes Benz\237\634
2\Audi\345\567
3\Ford\190\456
4\BMW\278\547
5\Toyota\123\364
6\Fiat\90\289
7\Daihatsu\120\450
8\Jeep\500\670
9\Seat\119\289
10\Mitsubishi\78\410

这就是我导入数据的方式,但表中填充了零。问题在哪里。我认为这个问题与FIELDS和LINES TERMINATED BY有关。

LOAD DATA LOCAL INFILE '/Users/anilyelin/Desktop/cars.csv'
INTO TABLE specs
FIELDS TERMINATED BY '\\'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS

输出如下所示:

mysql> select * from specs;
+----+-------+------------+-------+
| Id | Brand | Horsepower | Range |
+----+-------+------------+-------+
|  1 |       |          0 |     0 |
|  2 |       |          0 |     0 |
|  3 |       |          0 |     0 |
|  4 |       |          0 |     0 |
|  5 |       |          0 |     0 |
|  6 |       |          0 |     0 |
|  7 |       |          0 |     0 |
|  8 |       |          0 |     0 |
|  9 |       |          0 |     0 |
| 10 |       |          0 |     0 |
+----+-------+------------+-------+
10 rows in set (0,00 sec)

1 个答案:

答案 0 :(得分:0)

您应该尝试通过管道

替换字段分隔符\
LOAD DATA LOCAL INFILE '/Users/anilyelin/Desktop/cars.csv'
INTO TABLE specs
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS