load csv file into mysql using batch?
Since you are using load data local infile
and Loading Data into a Table using MySQL, there is a simple solution which is provided as part of this functionality which will help you to resolve this issue.
You simply want to be sure to include the ENCLOSED BY '"'
subclause as part of the FIELD
clause in the MySQL syntax you are using—
below is an example of this.
load data local infile "c:\\eqa\\project\\input.csv"
into table request
character set latin1
fields terminated by','
ENCLOSED BY '"'
lines terminated by'\n'
ignore 1 lines
This tells MySQL the input file has field values that are both enclosed by double quotes and separated by commas—All characters between double quotes will then be imported.
Further Resources
- Loading Data into a Table
-
LOAD DATA INFILE Syntax
LOAD DATA INFILE
can be used to read files obtained from external sources. For example, many programs can export data in comma-separated values (CSV) format, such that lines have fields separated by commas and enclosed within double quotation marks, with an initial line of column names. If the lines in such a file are terminated by carriage return/newline pairs, the statement shown here illustrates the field- and line-handling options you would use to load the file:LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
For input, the
ENCLOSED BY
character, if present, is stripped from the ends of field values. (This is true regardless of whetherOPTIONALLY
is specified;OPTIONALLY
has no effect on input interpretation.) Occurrences of theENCLOSED BY
character preceded by theESCAPED BY
character are interpreted as part of the current field value.If the field begins with the
ENCLOSED BY
character, instances of that character are recognized as terminating a field value only if followed by the field or lineTERMINATED BY
sequence. To avoid ambiguity, occurrences of theENCLOSED BY
character within a field value can be doubled and are interpreted as a single instance of the character. For example, ifENCLOSED BY '"'
is specified, quotation marks are handled as shown here:"The ""BIG"" boss" -> The "BIG" boss The "BIG" boss -> The "BIG" boss The ""BIG"" boss -> The ""BIG"" boss
source