Wednesday, September 23, 2009

Import data from csv file into MySQL table

Syntex can be run from MySQL Query Browser:

LOAD DATA LOCAL INFILE 'c:/a.csv' INTO TABLE neighborhood_boundary_Data fields terminated by ',' lines terminated by '\r\n' (neighborhoodid, lat, lon, zz, mm, state, county, city, name, regionid);

[Fields Terminated by ',']: This will be the character used in the CSV file for separating the data
[Lines Terminated by '\r\n']: I put \r to eliminate the enter key

With this particualr table I had an issue, the neighborhoodid field was having duplicate entries (in a MySQL one field with type integer is required to make it a index field) so when I was importing the data it was getting stuck when the value for neighborhoodID field was repeating, so I created an extra field in the table RecID with type integer and was indexed field and did not included that field in the list of fields in the Load data command and it worked.

No comments:

Post a Comment