Field separators
The default field separator for mysqlimport
is a tab character. You need to tell mysqlimport
to split fields on commas instead:
mysqlimport --fields-terminated-by=',' ...
This isn't quite enough, though, because if your CSV follows the spec, fields containing commas will be surrounded by double quotes. You need to tell mysqlimport
that it shouldn't treat commas inside quotes as field separators:
mysqlimport --fields-terminated-by=',' --fields-optionally-enclosed-by='"' ...
In some CSVs, all fields are double quoted, whether they contain commas or not. This is not the case with your data, but for future reference, mysqlimport
also provides the option
--fields-enclosed-by=string
I'm not sure, but I assume performance would be better on a CSV where all fields are quoted if you use --fields-enclosed-by='"'
instead of --fields-optionally-enclosed-by='"'
.
Line separators
The default line separator for mysqlimport
is a newline \n
. This should work for files generated on UNIX/Linux/Mac OS X, but if the EOL sequence in your file is something different (for example, \r\n
in most files created on Windows), you need to specify that as well:
mysqlimport --lines-terminated-by='\r\n'
Apparently, Wordpad uses '\r' as a line ending, as did Macs prior to Mac OS X.
Skipping column headers
Generally you don't want to import the names of your columns into your table, since the table already has its own column names. You can skip the first X number of lines in your file like this:
mysqlimport --ignore-lines=X
Putting it all together
To import a CSV file where
- fields that contain commas are double quoted
- fields that don't contain commas are not quoted
- lines end in
\r\n
- the first line contains column names, which you want to ignore
you would run
mysqlimport --fields-terminated-by=',' \
--fields-optionally-enclosed-by='"' \
--lines-terminated-by='\r\n' \
--ignore-lines=1 \
db_name table_name.csv
Note that mysqlimport
determines the table to import into by simply stripping off the file extension, so the above example will try to import the data from table_name.csv
into the table_name
table. Also, if you're running this on the server, you shouldn't need the --local
option, although the wording in the documentation is a little obtuse.
You can also use the equivalent SQL statement
LOAD DATA INFILE 'table_name.csv' INTO TABLE table_name
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
(mysqlimport
is just a command-line interface to LOAD DATA INFILE
)
See the docs for LOAD DATA INFILE
for more details.