Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL "LOAD DATA INFILE" and Missing Double Quotes

Tags:

mysql

csv

I'm trying to load a CSV into MySQL using the LOAD DATA INFILE technique. It's working fine, but I have a problem where some columns use double quotes and some do not.

Example:

something,123,something,"Bauer, Jack",123,something 

What happens is the commas inside the quotes break the import, so my data is all jacked up at the end. Not sure how to get the import to escape commas inside the double quotes.

mysql --user=<USER> --password=<PASS> -e "LOAD DATA INFILE '<FILENAME>' INTO TABLE <TABLENAME> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (col1, col2, col3, ...)" <DATABASE> 
like image 512
swt83 Avatar asked May 05 '11 22:05

swt83


People also ask

How do I enable MySQL load data?

To disable or enable it explicitly, use the --local-infile=0 or --local-infile[=1] option. For the mysqlimport client, local data loading is not used by default. To disable or enable it explicitly, use the --local=0 or --local[=1] option.

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

Load data into a table in MySQL and specify columns: LOAD DATA LOCAL INFILE 'file. csv' INTO TABLE t1 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@col1,@col2,@col3,@col4) set name=@col4,id=@col2 ; @col1,2,3,4 are variables to hold the csv file columns (assume 4 ) name,id are table columns.

How do I import a csv file into MySQL?

In the Format list, select CSV. Changing format-specific options. If the csv file is delimited by a character other than a comma or if there are other specifications to the csv files, we can change it in this portion. Click Go to start importing the csv file and the data will be successfully imported into MySQL.

What is load data infile in MySQL?

In MySQL database (or MariaDB), using “load data infile” command, you can upload data from a text file to tables. The load data infile command provides several flexible options to load various formats of data from text file to tables.


1 Answers

You need to execute the statement LOAD DATA INFILE with the additional option

FIELDS OPTIONALLY ENCLOSED BY '"' 

Thus the whole statement becoming

    LOAD DATA INFILE '<FILENAME>' INTO TABLE <TABLENAME> FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (col1, col2, col3, ...) 

For further readings, please consult the excellent MySQL Reference Manual e.g. for MySQL 5.1 GA.

like image 171
Andreas Krueger Avatar answered Sep 30 '22 19:09

Andreas Krueger