Import Excel Data into MySQL in 5 Easy Steps
This is probably nothing new to many, but I spent quite some time to figure it out so I thought I'd post my notes on it.
To import data from Excel (or any other program that can produce a text file) is very simple using the LOAD DATA command from the MySQL Command prompt.
Very quick and simple once you know it :)
Some notes from my own import - may not apply to you if you run a different language version, MySQL version, Excel version etc...
To import data from Excel (or any other program that can produce a text file) is very simple using the LOAD DATA command from the MySQL Command prompt.
- Save your Excel data as a csv file (In Excel 2007 using Save As)
- Check the saved file using a text editor such as Notepad to see what it actually looks like, i.e. what delimiter was used etc.
- Start the MySQL Command Prompt (I'm lazy so I usually do this from the MySQL Query Browser - Tools - MySQL Command Line Client to avoid having to enter username and password etc.)
- Enter this command:
LOAD DATA LOCAL INFILE 'C:\\temp\\yourfile.csv' INTO TABLE database.table FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (field1, field2); - Done!
Very quick and simple once you know it :)
Some notes from my own import - may not apply to you if you run a different language version, MySQL version, Excel version etc...
- TERMINATED BY - this is why I included step 2. I thought a csv would default to comma separated but at least in my case semicolon was the deafult
- ENCLOSED BY - my data was not enclosed by anything so I left this as empty string ''
- LINES TERMINATED BY - at first I tried with only '\n' but had to add the '\r' to get rid of a carriage return character being imported into the database
- Also make sure that if you do not import into the primary key field/column that it has auto increment on, otherwhise only the first row will be imported
Labels: MySQL



3 Comments:
hi
If the contents of the file is english, these 5 stepts are well.
Not working when the Excel sheet is having chinese characters & english characters.
i am using 5.0.45 DOS based.
i tried to import Excel(2003) file to MySql table. (Excel is having English letters & Chinese letters)
1. save excel file as .csv( comma delimeter)
2. in MySql, I typed :
set names utf8;
3.LOAD DATA LOCAL INFILE 'c:/test.csv'INTO TABLE test_table
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
After that i used "select * from test_table". The data are displayed as "????".(English char OK)
is it possible to display chinese characters in DOS mode.
what is the wrong? or any other method is there? Pl help me.
thanks
ramesh@mispl.com.sg
Even in English, the above will fail if your data has a comma in it. It will throw the column count off.
The key would be to get Excel to actually enclose the data in "".
Anyone know how to do that?
Hey,
I've another question: my datas have many columns and I'm too lazy to create a Table with the matching columns. Is there a way that MySQL automatically reads the columns? (the first line of the csv File has the column names)
Thanks and excuse my bad english ;)
Post a Comment
Links to this post:
Create a Link
<< Home