1

I am facing a problem inserting data from a CSV file into MySQL.

I have different types of CSV files, each with different columns. But, all files contain some common columns. I need to read only selected columns from the file.

Example CSV Columns

File 1: "Name","Mobile","Gender","Address","City","State"
File 2: "Name","Mobile","Distict","Operator","Address"
File 3: "Name","Mobile","Address"

My MySQL table structure is like this:

name | mobile | address

I am using the following method:

LOAD DATA INFILE '/path/to/file' INTO TABLE example FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (col1, col2, col3, ...);

I got few errors and I am unable to load data in accurately. Thanks in advance.

Will
  • 21,498
  • 11
  • 84
  • 98
Vinod
  • 11
  • 3
  • Possible duplicate of [How to skip columns in CSV file when importing into MySQL table using LOAD DATA INFILE?](http://stackoverflow.com/questions/2139069/how-to-skip-columns-in-csv-file-when-importing-into-mysql-table-using-load-data) – maxhb Dec 25 '15 at 10:13
  • I have clarified the formatting and grammar of your post a bit, which should help others understand the question better. Can you also edit your post and add the errors you're getting when you run the command you tried above? Also note that you were missing a letter in one of your `TERMINATED` command elements, but I corrected it in my edit. Is it possible that this was the problem? – Will Dec 27 '15 at 11:25

1 Answers1

0

If you are allowed to use other mysql tools. Navicat will help.

If you can only use command line.Then maybe use awk to combine all your csv file into a single file that have a fixed fields.And then do the load data trick.

amow
  • 2,126
  • 9
  • 19