Importing a CSV into MySql

We’re working on the new blenderbox site, and we’re moving from SQL server to a MySQL server and I wanted to quickly move our project data from our old server to the new one. I came across a great way to import data into a MySQL database directly from a CSV file.

Firstly, you’ll need the CSV file. To create that, I used the SQL Server Import and Export Wizard to save the table I wanted into an excel file.   I then opened the Excel File and deleted the column name row and saved the file out to a csv. Then I moved the file to my Ubuntu server and ran the following script from the mysql console.

load data local infile ‘csv_filename.csv’ into table destination_table
fields terminated by ‘,’
enclosed by ‘”‘
lines terminated by ‘\n’
(field_1, field_2, field_3, etc)

Where field_1, field_2, field_3, etc are the column names in my destination table that are the same order as the columns in my CSV file.

It worked like a charm and I hope it helps someone out there!

Comments are closed.