MySQL has a nice CSV import function LOAD DATA INFILE
.
I have a large dataset that needs to be imported from CSV on a regular basis, so this feature is exactly what I need. I've got a working script that imports my data perfectly.
.....except.... I don't know in advance what the end-of-line terminator will be.
My SQL code currently looks something like this:
LOAD DATA INFILE '{fileName}'
INTO TABLE {importTable}
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
( {fieldList} );
This works great for some import files.
However, the import data is coming from multiple sources. Some of them have the \n
terminator; others have \r\n
. I can't predict which one I'll have.
Is there a way using LOAD DATA INFILE
to specify that my lines may be terminated with either \n
or \r\n
? How do I deal with this?
\n
(most *nix systems, including OS X) and\r\n
(Windows)--in which case @Devart's answer looks perfect--or might you encounter other line termination sequences such as\n\r
(e.g. from RISC OS),\r
(e.g. Mac OS 9) and others? – Graph