Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql load data infile it contain more data than there were input column

Tags:

mysql

csv

I'm a new to mysql, I try load csv file to mysql. the csv like:

1,"a,b"
2,bc
3,d

the table like this:

create table test(ind varchar(10),var varchar(20));

when I load this csv file:

load data infile 'test.csv' into table test
fields terminated by ',' ;

I change this the warning:

row 1 was truncated: it contained more data than there were input columns I try this:

load data infile 'test.csv' into table test
fields terminated by ','
optionally enclosed by '"'

it doesn't work.

the common of "a,b" cause this error. but I don't know how to solve this question.

like image 609
pang2016 Avatar asked Mar 23 '17 03:03

pang2016


People also ask

Which is more efficient load data insert?

When loading a table from a text file, use LOAD DATA . This is usually 20 times faster than using INSERT statements. See Section 13.2. 7, “LOAD DATA Statement”.

How do I insert selected columns from a CSV file to a MySQL database using load data infile?

The code is like this: LOAD DATA INFILE '/path/filename. csv' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (column_name3, column_name5); Here you go with adding data to only two columns(you can choose them with the name of the column) to the table.

What is load data infile in MySQL?

Using the LOAD DATA statement, you can insert the contents of a file (from the server or a host) into a MySQL table. If you use the LOCAL clause, you can upload the local files contents int to a table.

What is Fields escaped by?

Use \' to escape instances of tab, newline or \' that occur within field values. Write newlines at the ends of lines. Note that to write FIELDS ESCAPED BY '\\', you must specify two backslashes for the value to be read as a single backslash.


1 Answers

It sounds like maybe LOAD DATA isn't properly picking up on your line breaks. Try adding LINES TERMINATED BY ... to your call:

LOAD DATA INFILE 'test.csv' INTO TABLE test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'         -- use '\n' if on Linux
(ind, var)

With the above call, MySQL should not view the comma inside the first quoted term "a,b" as being a field separator, but rather just part of the text of that column.

like image 167
Tim Biegeleisen Avatar answered Sep 19 '22 00:09

Tim Biegeleisen