Before importing the file, you must need to prepare the following:
- A database table to which the data from the file will be imported.
- A CSV file with data that matches with the number of columns of the
table and the type of data in each column.
- The account, which connects to the MySQL database server, has FILE
and INSERT privileges.
Suppose we have following table :
CREATE TABLE USING FOLLOWING QUERY :
CREATE TABLE IF NOT EXISTS `survey` (
`projectId` bigint(20) NOT NULL,
`surveyId` bigint(20) NOT NULL,
`views` bigint(20) NOT NULL,
`dateTime` datetime NOT NULL
);
YOUR CSV FILE MUST BE PROPERLY FORMATTED FOR EXAMPLE SEE FOLLOWING
ATTACHED IMAGE :
If every thing is fine.. Please execute following query to LOAD DATA FROM CSV FILE :
NOTE : Please add absolute path of your CSV file
LOAD DATA INFILE '/var/www/csv/data.csv'
INTO TABLE survey
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
If everything has done. you have exported data from CSV to table successfully