简体   繁体   中英

MySQL - LOAD DATA INFILE problem

I am trying to import some data from a csv file into a mysql table using the LOAD DATA LOCAL INFILE command as below;

DROP TABLE IF EXISTS rctemp;
CREATE TABLE IF NOT EXISTS rctemp (`region` varchar(32) NOT NULL,
  `reportSuite` varchar(32) NOT NULL,
  `PageImpressions` int(11) NOT NULL,
  `UniqueUsers` int(11) NOT NULL);

LOAD DATA LOCAL INFILE 'main.csv'
INTO TABLE rctemp
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(region, reportSuite, PageImpressions ,UniqueUsers);

The sql runs and produces no errors when run from command line but it doesn't insert any data into the table. It creates the table perfectly fine but fails to add any data from the csv.

I had the same problem, but I tried this, I wrote the comand line like this (focus to your project) in the file to load.

"region","reportSuite",12,23
"anotherregion","anotherreportSuite",13,24

Now when you run the comand write like this

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

Don't write ENCLOSED BY '"'
And that is all.

It worked for me:D

I've been working on importing CSV files recently and have had the same issue.

One thing to check is the line termination. Another is the field termination, it is possible to use 'optionally terminated by "' rather than just 'terminated by'.

Turns out it was missing the 'optional' ENCLOSED BY '"'

added that and it works fine now!

LOAD DATA LOCAL INFILE 'sport.csv'
INTO TABLE rctemp
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM