CSV File Import into MySQL Table with Column Header Naming
Importing a CSV file into a MySQL table can be achieved with a SQL LOAD DATA command. To utilize this method, ensure the CSV file's first row comprises the column headers.
The syntax for importing the CSV file is as follows:
LOAD DATA LOCAL INFILE 'uniq.csv' INTO TABLE tblUniq FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (uniqName, uniqCity, uniqComments)
By including the 'IGNORE 1 LINES' option, the SQL command skips the first line of the CSV file, effectively assigning the column headers from the second line onwards.
Additional Considerations
Prior to importing the CSV file, ensure to export it from an Excel file and remove any headers from the generated CSV. Additionally, eliminate any empty data present at the end of the CSV file.
Note:
The SQL LOAD DATA command can also be used to ignore specific lines within the CSV file, providing granular control over the import process.
The above is the detailed content of How to Import a CSV File into MySQL Using LOAD DATA, Handling Headers and Empty Data?. For more information, please follow other related articles on the PHP Chinese website!