mysql> LOAD DATA INFILE ‘/tmp/filename.csv’ replace INTO TABLE [table name] FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ (field1,field2,field3);
To load a CSV file into a table in MySQL, you can use the LOAD DATA INFILE
statement. Here’s a basic example of how to do it:
LOAD DATA INFILE 'path_to_your_csv_file.csv'
INTO TABLE your_table_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS; -- If your CSV file contains a header row
Replace 'path_to_your_csv_file.csv'
with the path to your CSV file, and your_table_name
with the name of the table you want to load the data into. Adjust the FIELDS TERMINATED BY
and ENCLOSED BY
clauses according to your CSV file’s structure.
This statement assumes your MySQL user has the necessary permissions to access the file. If your MySQL server is running on a Unix-like system and you encounter permission issues, you may need to ensure that the MySQL server process has permissions to access the directory containing the CSV file. Alternatively, you can place the CSV file in a directory that is accessible to the MySQL server process.
Make sure that the structure of the CSV file matches the structure of the table you’re loading the data into, including the order of columns.