1

my text file is like this. https://drive.google.com/open?id=1faW_OkO7_VoEQL_gndFIOrJv2e4Ycuzo

and my table is here.

CREATE TABLE news(
    num INT auto_increment primary key,
    link VARCHAR(150),
    date INT,
    title VARCHAR(150) unique,
    description TEXT 
);

i try

LOAD DATA INFILE 'test.txt'
    INTO TABLE news
    CHARACTER SET utf8mb4
    FIELDS
    TERMINATED BY ', ' OPTIONALLY ENCLOSED BY '"' 
    LINES
    TERMINATED BY '\n' (link, date, title, description);

but it not working what is my mistake?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
sang oh
  • 66
  • 1
  • 8

2 Answers2

1

Your input file has four columns looking like this

url,month,title,"description": data 

A CSV import will include the word "description": at the beginning of each description column.

So remove OPTIONALLY ENCLOSED BY '"' from your command, and you should be all set.

By the way, make sure your table definition mentions the utf8mb4 character set (or some characters from your input may not get represented correctly). For best results, don't rely on the server defaults to choose character set.

CREATE TABLE news(
            num INT auto_increment primary key,
           link VARCHAR(150),
           date INT,
           title VARCHAR(150) unique,
    description TEXT 
)
COLLATE 'utfmb4_general_ci';
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • "description": is my fault. i will remove this.!! thanks. i'm normalization description field with no special character so utfmb4 don't need to me! – sang oh Sep 22 '18 at 13:38
0

This error is because of Optionally Enclosed by '. I have faced the same issue before and got resolved it by making a simple adjustment. Just use the Enclosed by clause immediately after 'Fields' keyword.
Hence your code would be like something:

LOAD DATA INFILE 'test.txt'
INTO TABLE news
CHARACTER SET utf8mb4
FIELDS OPTIONALLY ENCLOSED BY '"' 
TERMINATED BY ', ' 
LINES
TERMINATED BY '\n' (link, date, title, description);
mastisa
  • 1,875
  • 3
  • 21
  • 39