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'
Related
I have a csv where each row starts with quotation marks and ends with them too. How do I ignore the quotations at the start and end of the row when loading the csv into a table?
LOAD DATA LOCAL INFILE '/path/data.csv'
INTO TABLE test1
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\"\n'
IGNORE 1 ROWS;
I have tried
OPTIONALLY ENCLOSED BY '"'
but this refers to each specific field and not the entire row.
As commented by Shadow and Barmar, the answer lies in the documentation :
If all the input lines have a common prefix that you want to ignore, you can use LINES STARTING BY 'prefix_string' to skip the prefix and anything before it. If a line does not include the prefix, the entire line is skipped. [...] The FIELDS TERMINATED BY, LINES STARTING BY, and LINES TERMINATED BY values can be more than one character.
Hence, use :
LOAD DATA LOCAL INFILE '/path/data.csv'
INTO TABLE test1
FIELDS TERMINATED BY ';'
LINES STARTING BY '"'
LINES TERMINATED BY '"\n'
IGNORE 1 ROWS;
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.
I am trying to load large text files that are delimited by DEL. Using Coldfusion I can do chr(127), but that doesn't seem to work here
LOAD DATA LOCAL INFILE "C:\\inetpub\\vhosts\\mysite.com\\dataload.data"
INTO TABLE tbl_data
COLUMNS TERMINATED BY '\0x7F'
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
I have been searching and trying a number of things that look like they can be DEL characters. Does anyone have any suggestions for the COLUMNS TERMINATED BY line?
After a lot of searching, I found that my original statement was close. The answer is:
COLUMNS TERMINATED BY X'7F'
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 ;
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';