An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Monday, August 31, 2009

How to import CSV Files into MySQL tables?

No comments :
Usually, web applications will write into a CSV file that will be sent to Data Warehouse where the data would be further processed for analytical purposes. However, in specific cases we might be receiving the processed data in CSV files and we might have to load it into the OLTP (OnLine Transaction Processing) systems.

This post is to help you load the CSV (structured data) into your Database

Problem/Query:
How to import CSV Files into MySQL tables?

Scenario:
You have a CSV file coming into a specific folder from a different application and you have to load it into your MySQL database.

Solution:


Using "Load Data" statement anyone can import a csv file into the mysql database table without hassles. This is the best way of importing bulk data into the database. Huge amount of excel data shall be converted into csv and shall be imported into mysql database

Example:
LOAD DATA INFILE '/path/to/filename.csv' INTO TABLE TABLE_NAME FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'

LOAD DATA INFILE 'file.csv' INTO TABLE TABLE_NAME FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Trivia:
LOAD DATA INFILE is used to reverse the process of SELECT ... INTO OUTFILE(To write data from a table to a file)

Import CSV Files - MySQL
Keywords: CSV MySQL, MySQL CSV, CSV Files Import, CSV Files Import into MySQL

No comments :