user2653686
user2653686

Reputation: 31

I can import CSV file with CSV option but doesnt work for CSV with LOAD DATA

I can import CSV file with CSV option but doesnt work for CSV with LOAD DATA. I get the error

Error: 1083 - Field separator argument is not what is expected

...although I am doing everything same,uploading the same file to same table.

Upvotes: 3

Views: 1636

Answers (1)

ktm5124
ktm5124

Reputation: 12123

You are probably forgetting to set the field separator to a comma. Here's an example command for loading a CSV file from the MySQL documentation.

LOAD DATA INFILE can be used to read files obtained from external sources. For example, many programs can export data in comma-separated values (CSV) format, such that lines have fields separated by commas and enclosed within double quotation marks, with an initial line of column names. If the lines in such a file are terminated by carriage return/newline pairs, the statement shown here illustrates the field- and line-handling options you would use to load the file:

LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES;

Some notes:

  • If your first row doesn't contain headers, you should omit the IGNORE 1 LINES.
  • If your fields aren't enclosed by quotation marks, you can omit that too.
  • Lastly, if your file is local, you should be using LOAD DATA LOCAL INFILE.

Upvotes: 2

Related Questions