Mysql Load data infile ignore 1 lines not working - mysql

I want to import a CSV file to my Mysql table using Load data infile: here's my current code :
LOAD DATA INFILE '../myfile.csv'
INTO TABLE data
FIELDS
TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n' IGNORE 1 LINES (#f1,#f2...) set `f1`=#f1,...,fk=13 ;
This is the first part of the csv file :
Timestamp,FromName,FromID,FromA,FromAID,FromURL,ToCName,ToCID,ToCTarget,ToAName,ToAID,ToAURL,UUID,Model,OS,Country,Type,Value
"2012-10-29 07:02:20","NH","4f7898654fgh02","Halloween Game","589754hj67d00021","78643609","","","","Game 1™","4f754hj67d00014","58975449","988675ffgh555f3284530","iPhone","5.1.1","GB","cpi","0.5"
this gives me a 0 rows inserted, but if i replace IGNORE 1 LINES with IGNORE 0 LINES the file is imported successfully (of course without ignoring the first line).
Any help is appreciated.

Removing this line solved the problem :
ESCAPED BY '"'
working code :
LOAD DATA INFILE '../myfile.csv'
INTO TABLE data
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n' IGNORE 1 LINES (#f1,#f2...) set `f1`=#f1,...,fk=13 ;

Related

Mysql "LOAD DATA LOCAL INFILE" should not stop execution when file does not exist

We have several CSV files which we load into our database. They are provided by an external party. Because of reasons unknown to me they sometimes split data into two files, sometimes it is one file. How can I load the "optional" file (here a_2.txt) only if the files exists or convince LOAD DATA INFILE to not stop the execution of my script?
LOAD DATA LOCAL INFILE "/home/foo/a_1.txt"
INTO TABLE
``table_a`
FIELDS TERMINATED BY '|' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
LOAD DATA LOCAL INFILE "/home/foo/a_2.txt"
INTO TABLE
`table_a`
FIELDS TERMINATED BY '|' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
LOAD DATA LOCAL INFILE "/home/foo/b.txt"
INTO TABLE
`table_b`
FIELDS TERMINATED BY '|' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;

LOAD DATA INFILE and Double Quotes

When I export table to csv, fputcsv adds double quotes to values with space, for example:
day|night|summer|winter
something|123|something|"Bauer Jack"
foo|bla|5|dooper
I figured out I cannot avoid that.
The problem becomes when I try to import this csv with LOAD DATA INFILE, it does not import a line that contains double quotes. So line with "Bauer Jack" example is not imported into mysql.
$query = <<<eof
LOAD DATA LOCAL INFILE '$filename' INTO TABLE `table_name`
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
eof;
Can you suggest a solution? Why is line with double quotes not imported?
Everything works fine when I remove double quotes from csv file.
Try putting the OPTIONALLY ENCLOSED BY clause first.
$query = <<<eof
LOAD DATA LOCAL INFILE '$filename' INTO TABLE `table_name`
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY '|'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
eof;

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

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.
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.

LOAD DATA LOCAL INFILE not working - File not found (MySQL Workbench)

I am using the below command to load an excel file to MySQL server:
LOAD DATA LOCAL INFILE 'GPMO_Resource_Download_report_CSV'
INTO TABLE gpmo_resource_download_report_1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';
I have this file on the same machine at the below path:
C:\Users\aprakas2\Desktop\Task\GPMO_Resource_Download_report_CSV
Please suggest me how should I load this file ?
I think these SQL will useful to you.
If file has header row
LOAD DATA INFILE 'C:/Users/aprakas2/Desktop/Task/GPMO_Resource_Download_report_CSV'
INTO TABLE gpmo_resource_download_report_1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
If file has no header row
LOAD DATA INFILE 'C:/Users/aprakas2/Desktop/Task/GPMO_Resource_Download_report_CSV'
INTO TABLE gpmo_resource_download_report_1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Thank you.

Inserting CSV into mysql

this may just be the dumbest question I have asked yet. But I can not figure this out. I am trying to import a CSV into my mysql. But I can not get these areas correct:
Columns separated with:
Columns enclosed with:
Columns escaped with:
Lines terminated with:
Here is an example row in mycsv:
AA|Armed Forces Americas|US|United States
what would I place in each one?
If you have table that match the column configuration of the file what you do is:
LOAD DATA INFILE '<full file path>`
INTO TABLE <tbl_name>
FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
If some of the fields may contain | then these fields should be enclosed by " and then the last clause becomes:
FIELDS TERMINATED BY '|' ENCLOSED BY '"' TERMINATED BY '\n'