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>
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With