5

我有一张名为 city 的表:

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| country_id | mediumint(9) | NO   | MUL | NULL    |                |
| region_id  | bigint(20)   | NO   | MUL | NULL    |                |
| city       | varchar(45)  | NO   |     | NULL    |                |
| latitude   | float(18,2)  | NO   |     | NULL    |                |
| longitude  | float(18,2)  | NO   |     | NULL    |                |
| timezone   | varchar(10)  | NO   |     | NULL    |                |
| dma_id     | mediumint(9) | YES  |     | NULL    |                |
| code       | varchar(4)   | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

我有一个要导入的简单文件(只是一个测试文件):

"id","country_id","region_id","city","latitude","longitude","timezone","dma_id","code"
42231,1,833,"Herat","34.333","62.2","+04:30",0,"HERA"
5976,1,835,"Kabul","34.517","69.183","+04:50",0,"KABU"
42230,1,852,"Mazar-e Sharif","36.7","67.1","+4:30",0,"MSHA"
42412,2,983,"Korce","40.6162","20.7779","+01:00",0,"KORC"
5977,2,1011,"Tirane","41.333","19.833","+01:00",0,"TIRA"
5978,3,856,"Algiers","36.763","3.051","+01:00",0,"ALGI"
5981,3,858,"Skikda","36.879","6.907","+01:00",0,"SKIK"
5980,3,861,"Oran","35.691","-0.642","+01:00",0,"ORAN"

我运行这个命令:

LOAD DATA LOCAL INFILE 'cities_test.txt' INTO TABLE city FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;

输出:

Query OK, 0 rows affected (0.00 sec)
Records: 0  Deleted: 0  Skipped: 0  Warnings: 0

没有插入记录,我不知道为什么。

有任何想法吗?

谢谢!

杰米

4

4 回答 4

22

解决了。愚蠢的错误。

不得不改变这个:

LINES TERMINATED BY '\r\n'

对此:

LINES TERMINATED BY '\n'

:-)

于 2011-01-11T14:24:05.480 回答
2

我有同样的问题,但我试试这个,删除第一行

`("id","country_id","region_id","city,"latitude","longitude",
                  "timezone","dma_id","code")` in your file to import.

现在,当您运行命令时,像这样写

mysql> LOAD DATA LOCAL 
    INFILE 'cities_test.txt' 
    INTO TABLE city FIELDS TERMINATED BY ','  
    LINES TERMINATED BY '\n';

仅此而已。

它对我有用:D

于 2013-06-11T04:54:16.160 回答
1

我在mac上遇到了同样的问题,如果你使用的是mac,试试这个

LOAD DATA INFILE 'sqlScript1.txt' INTO TABLE USER
FIELDS TERMINATED BY ','  LINES STARTING BY '\r';
于 2013-11-02T16:34:12.060 回答
0

对我来说,在 Mac 上工作的是

LOAD DATA LOCAL 
    INFILE 'cities_test.txt' 
    INTO TABLE city FIELDS TERMINATED BY ','  
    LINES TERMINATED BY '\r';

由于 Mac 使用回车符作为换行符,因此您必须使用'/r'

于 2017-01-27T14:01:59.877 回答