Before importing the file, you 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 the following table:
![Enter image description here]()
Create the table using the 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 the following attached image:
![Enter image description here]()
If everything is fine, please execute the following query to load data from the CSV file:
Note: Please add the 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 been done, you have exported data from the CSV file to the table successfully.