LOAD DATA INFILE and Double Quotes - mysql

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;

Related

LibreOffice Base Macro run MySQL command ENCLOSED BY (and the enclosed are by qotation markes aka "

Im doing a macro in Libreoffice extract a txt file to mysql but all data has "double quotes".
in mysql terminal this command works fine.
LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' ENCLOSED BY '"' IGNORE 1 LINES;
in macro like this it works oStatement.execute("LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' IGNORE 1 LINES;")
but if i add ENCLOSED BY '"' I get erro.
oStatement.execute("LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' ENCLOSED BY '"' IGNORE 1 LINES;")
Thank you for your help
I found the solution
oStatement.execute("LOAD DATA INFILE '/var/lib/mysql-files/Stock.txt' INTO TABLE StockXLS FIELDS TERMINATED BY ';' ENCLOSED BY '" & """" & "' IGNORE 1 LINES;")

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.

Not able to import price column properly in MySQL from a CSV file

I've a csv file containing data in this format:
SATURN,6459,"50,486",27184
I'm using this command to import this file into the table:
LOAD DATA LOCAL INFILE 'D:\\temp.csv' INTO TABLE `test`.`tmp` FIELDS ESCAPED BY '\\'
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
(`description`,`file_no`,`net`,`run_no`);
all the fields are being imported correctly but the net column always having the data like
50.00 // it should be 50,486
Data type of this column is Decimal(10,2). I've also tried Numeric(10,2) but no luck. Any help is highly appreciated. Thanks a lot in advance.
Please have a try with this one:
LOAD DATA LOCAL INFILE 'D:\\temp.csv' INTO TABLE `test`.`tmp` FIELDS ESCAPED BY '\\'
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
(`description`, `file_no`, #a_variable, `run_no`)
SET `net` = REPLACE(#a_variable, ',', '');
MySQL cannot deal with commas as 1000 separators when inserting numbers. You need to parse out these commas before loading.
If you need the numbers formatted this way when querying, use FORMAT(). For examples see Apply comma to number field in MySQL

Mysql Load data infile ignore 1 lines not working

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 ;

Using MySQL OUTFILE with non-simple data

I've been using the following command to expert mysql data to a csv file.
SELECT * INTO OUTFILE 'output.csv' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM table1;
It works for simple tables with simple data. However, if the table contains html tags, double quotes, single quotes, ascii characters etc, it does not work propertly, i.e. it will put tabs and new lines in incorrect places, breaking up data where it shouldn't. How can the sql script above be improved to export data with html?
I have tried SELECT...INTO OUTFILE statement, and then LOAD DATA INFILE statement, everything is OK, the HTML text was exported/imported without any mistakes (on MySQL 5.5).
Try to add ENCLOSED BY option, it should help you, e.g. -
SELECT *
INTO OUTFILE 'output.csv'
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM
table1;
LOAD DATA INFILE 'output.csv'
INTO TABLE table1
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n';