开发者_如何转开发Possible Duplicate:
Tool for importing CSV files into MySQL database?
A guy at work gave me a .csv file with thousands of records in it. There's about 5 columns (out of 20) that I would love to get inserted into a mysql database.
Any idea how I might go about that?
Using LOAD DATA INFILE
. The example in the docs for CSV is:
LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
You should remove the IGNORE 1 LINES
clause if there is no header row in the CSV data.
Also, note that the order of the data in the file should match the order of the columns in the table. If they do not, you will need to specify the order like this:
LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
(column1, column2, ...)
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
use the LOAD DATA or BULK INSERT command
精彩评论