Import Excel Data into MySQL in 5 Easy Steps
Feb 29
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.
- 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);
[Edit: Make sure to check your single quotes (') and double quotes (") if you copy and paste this code - it seems WordPress is changing them into some similar but different characters] - 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
[Update: I had some trouble getting double quotation marks etc when exporting some Excel data and found a few old but useful Excel export macros in VBA]

Apr 15, 2008 @ 08:51:00
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
Apr 24, 2008 @ 00:01:00
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?
Apr 29, 2008 @ 11:22:00
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
Jul 04, 2008 @ 16:38:00
hi, do someone know if i can import more than 50000 records in a csv with this fuction “LOAD DATA LOCAL INFILE”? i`d really appreciate if someone reply me to dorian_andres@hotmail.com, thanks
Jul 06, 2008 @ 18:32:00
Yes, you should be able to load more than 50k records into a mysql table, I’ve been using it to load 100s of 1000s of reords into mysql tables with no issues
Jul 09, 2008 @ 01:09:00
Does anyone have an answer to the question posted on “Tue Apr 29, 12:22:00 PM CEST”
“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
”
Jul 24, 2008 @ 05:04:00
ello,
When i am trying to run this query
LOAD DATA LOCAL INFILE '\taxonomy.csv' INTO TABLE taxonomy FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (Tax_Id,Family,Genus,SpecificEpithet,InfraSpecificEpithet,InfraSpecificRank,AuthorYearOfScientificName);
i am getting this error.
File ' axonomy.csv' not found (Errcode: 22)
I have placed the taxonomy.csv file in the Mysql\bin folder and also set the class path….
Can any one tell me how to fix this????
Thanks&Regards,
PinazKale.
Jul 24, 2008 @ 08:07:00
Pinu – you should use a double backslash, ie ‘\\taxonomy.csv’
Jul 24, 2008 @ 08:12:00
Anonymous about chinese. I have no idea but I would suggest you try different code pages/character set to see if you can get it to work.
Here is some info on chinese codepages:
http://dev.mysql.com/doc/refman/5.0/en/faqs-cjk.html
http://dev.mysql.com/doc/refman/5.0/en/faqs-cjk.html#qandaitem-30-11-1
Jul 25, 2008 @ 00:07:00
Hey hi ,
I tried using \\ but its still giving me the same error..
I have also placed the .csv file in mysql\bin folder…
I tried also giving the entire file path….
Does this have to do with some thing like local_infile mode enabled???
Sep 27, 2008 @ 13:00:00
Thanks a lot for the straight-forward approach – I’ve been looking around for tools that would do the same and was pleasantly surprised to find such an easy way out – thanks a bunch!
Oct 07, 2008 @ 22:09:00
Does it work to run this mysql script in a php file and execute the sql-statement just like i normal select-statement? Or did I must be connected thought the mysql-query browser on my computer?
Oct 08, 2008 @ 14:50:00
hello!
how can I get the id’s of the inserted rows after import?
Nov 12, 2008 @ 20:26:00
Hi,
I had the same problem with \\ and \ not working in windows…use the other use the / it worked for me and I’m also in a windows environment.
thanks,
Parag
Apr 02, 2009 @ 05:13:13
Error
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem
ERROR: Unknown Punctuation String @ 26
STR: :\\
SQL: LOAD DATA LOCAL INFILE ‘C:\\Book1.csv’ INTO TABLE exceldbase.exceltable FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\r\n’ (Fname, Mname, Lname, Age)
SQL query:
LOAD DATA LOCAL INFILE ‘C:\\Book1.csv’ INTO TABLE exceldbase.exceltable FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\r\n’ (Fname, Mname, Lname, Age)
MySQL said: Documentation
#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘C:\\Book1.csv’ INTO TABLE exceldbase.exceltable FIELDS TERMINATED BY ‘,‒ at line 1
Apr 16, 2009 @ 10:29:07
I have the same problem with Fred. What version of mysql server should I use?
Apr 16, 2009 @ 11:28:56
Una cosa sólo, si eres español, para que te coja eñe (ñ), tildes y acentos, asegúrate de que el archivo CSV está en el mismo collate que la tabla destino, yo por ejemplo en la tabla los campos son utf8_general_ci, y el archivo CSV con el notepad++ lo he pasado a UTF8.
saludos y gran post, me has salvado!
Apr 20, 2009 @ 03:53:20
This works fine from the above poster.
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’;
May 14, 2009 @ 07:17:13
Fred,
I initially had the same problem as yours, make sure you use the correct single quote. Do not just copy paste from this page.
LOAD DATA LOCAL INFILE ‘C:/filename.csv’
INTO TABLE tablename
FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\r\n’ (field1, field2, etc);
May 14, 2009 @ 22:44:10
Have you tried this?
pervasivedatatools.com/datatools/loaders/product/mysql-uploader
It works.
May 19, 2009 @ 20:33:28
How do we insert a value from a sequence for each row of data insert?
May 21, 2009 @ 12:17:33
Hey Hi,
i. i m creating excel file and save in csv(comma delimate format)
But when i type the load data function in mysql command prompt then fallowing problem occure please resolve my problem
Tanks in advance
mysql> LOAD DATA LOCAL INFILE ‘C:\\sample.csv’ INTO TABLE amit.click_records FIE
LDS TERMINATED BY ‘;’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’ (id,keyword,adver
tiser_id,advertisement_id,campaign_id,clicks,click_date);
Query OK, 1 row affected, 21 warnings (0.01 sec)
Records: 3 Deleted: 0 Skipped: 2 Warnings: 20
The message is that 1 row affected but when i m checking the table records by using select query then thre will no records are added in table.
With Regards,
Sandip Khairnar
Jun 15, 2009 @ 13:06:03
Hey Hi,
It seems to be very usefull code, can work with no error. thanks for your suggestions.
Bye & take care!!!!!!!!!!!
Thanks & regards
Deepak Toppin
Jun 15, 2009 @ 13:08:35
LOAD DATA LOCAL INFILE ‘D:\Book2.csv’ INTO TABLE emp.employee FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ (empname,eadd);
Jun 16, 2009 @ 02:47:38
Check out this cool website that converts excel sheets into mysql tables.
Jun 26, 2009 @ 10:20:13
When the Excel file is distributed into multiple Sheets then you have to Save each sheet as an CSV, and import each of them to get all the Sheets in table.
Jul 05, 2009 @ 15:22:35
@arieatuw, thanks dude. :p
Aug 06, 2009 @ 09:03:40
i’m working on website using ASP.NET 2.0 (C# ) & mysql combination
my mysql command LOAD DATA LOCAL INFILE ‘C:/Documents and Settings/User/Desktop/MSN.csv’ IGNORE INTO TABLE m1 FIELDS TERMINATED BY ‘|’ LINES STARTING BY ‘’ TERMINATED BY ‘\r\n’ IGNORE 1 LINES; “;
works fine on my system,
when i upload this page to the server
file path will become ‘C:\Documents and Settings\User\Desktop\MSN.csv’ & file not found error will come, if anybody know’s the answer please help me
Aug 07, 2009 @ 09:46:50
@arieatuw,
Thanks for the pointers!
Sep 15, 2009 @ 19:53:52
THANK YOU!!!
This three lines helped me so so much!!
THANK YOU!!
Sep 18, 2009 @ 04:53:00
Another method.
Another way you can do this, if you don’t want to create the table from scratch or have many columns like Robert.
Create a Access database.
Import CSV into access, then export to ODBC.
I’ve done this successfully in the past, currently it is crashing access for me..
Its worth a try I guess.
Sep 22, 2009 @ 19:10:57
Hi ,
I had data in Excel.
Opend Access. Imported data from Excel.
Used MySQL Migration Toolkit , where u chose to migrate from access to mysql. Worked for me.
Sep 24, 2009 @ 15:14:54
i have problem with import csv into mysql table . while i import csv column
name only imported into mysql table. i can’t get the value . how to resolve.
could anyone help me.
Sep 28, 2009 @ 17:34:47
Be aware too that sometimes MySQL doesn’t like empty cells. In other words, you’ll probably have to find and replace out all the empty values with NULL values. To complicate things a little further, NULL isn’t actually a string, so will have to exist in the CSV separated by the commas but not encased in the double quote marks.
Oct 07, 2009 @ 12:32:12
i have two tables in my c: 1)filesandpath.csv 2)report_2.csv . and also i have two empty tables . and i can able to import filesandpath.csv into first empty table(filesandpath table) both has three field. but wht my problem is
i can’t import report_2.csv into onemore mysql table report_2.csv contains 9 rows i want to import first three rows only . because i create three fields in report_2 table. but my problem is only row name is imported into mysql table. i can’t get the value could anyone reply i strugled long time.
Advance thanks for your reply
Oct 14, 2009 @ 21:17:17
As for ignoring the first line of a data file (e.g., containing headers), use IGNORE x LINES command where x = 1.
Example :
LOAD DATA INFILE ‘/tmp/test.txt’ INTO TABLE test IGNORE 1 LINES;
Source :
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
Nov 27, 2009 @ 17:13:56
It helped me a lot. I was able to successfully imported excel sheet data into mysql using this method
Dec 27, 2009 @ 09:15:43
Hi
I tried out alll the above given test querieload excel data to my table but nothing worked for my caseI .Finally I tried out the below steps to achieve.
copy past the excel data as it is to a text file save your file to c: \\ eg c:\\test.text and run the below query
LOAD DATA LOCAL INFILE “c:\\test.txt” INTO TABLE mysqltable FIELDS TERMINATED BY “\t” LINES TERMINATED BY “\n”;
Jan 12, 2010 @ 08:08:15
Прикольно. Автору, как говорится, респект.
Feb 12, 2010 @ 22:30:23
Not working for me either?
Apr 05, 2010 @ 13:50:25
Worked successfully. Just have to figure out what should be used in TERMINATED BY TAG and other tags.. Thanks
Apr 06, 2010 @ 12:15:37
Hi,
Very IMPORTANT :
The steps were simple and when i executed the command in Mysql query browser it gave me an error on invalid syntax….
So remember if you copy the code and paste it…it wont work. as the Single quotes are changed to something else….just re-type the single quote
Apr 08, 2010 @ 10:47:30
what if i need to load to two different tables from 1 csv file?
May 04, 2010 @ 23:25:18
Thanks for the steps/tips! It helped me upload information quickly and painlessly (plus I didn’t need to do any more research). You’ve been a big help!
May 11, 2010 @ 07:20:56
Also, if you are importing dat make sure not to have other characters. like sally’s, you must put it as sallys or you will get errors
May 14, 2010 @ 15:20:33
Dear,
If you want update two different tables (e.g. foreign key relation) you can
use dbTube.org. The tool can read native Excel Files and it is an
PHP script. No coding required.
@iza: maybe this solves your problem.
Mark
Jun 07, 2010 @ 07:23:40
Can anybody help me out wit this problem???
Given:
1) Two LAN located at physically different location
2) First LAN (LAN1)
3) Second LAN (LAN2)
4) On LAN2 Server, there is an excel File xyz.xls.
5) On LAN1, there is an SQL server. (OS Windows Server 2003).
Problem:
How do I import data from the excel file (on LAN2) to the SQL Server (on LAN1) without converting the excel file into csv file???
Jul 12, 2010 @ 09:14:01
Thanks for sharing this info, I need some help, can someone please tell me how to import data from mysql to excel.
I need it very urgently.
Regards,
sonia
Jul 12, 2010 @ 12:28:36
@sonia,
Mysql natively supports export to excel 2000 format through phpmyadmin interface.
Jul 12, 2010 @ 12:30:52
@imz,
->answer to the last problem.
Try creating a ODBC datasource to connect to excel workbook. Windows comes with its own ODBC driver for Excel allowing you to use your excel workbook as DBMS
Jul 14, 2010 @ 09:32:26
Very Well done!
But,1 more point I want to include!
If the contents contain ‘,’ then replace all to something like ‘ ZZZZZ ‘ or anything similar
and then import it
After importing,using php codes,replace the ‘ ZZZZZ’ with “,” again.
Thankk You,
Have a nice Day!
Jul 14, 2010 @ 09:34:20
Dear sonia,
To import data to Excel
Open Phpmyadmin
Click Export and then Select Excel 2000
and you r done!
Aug 19, 2010 @ 06:40:33
Thank for mysql importing tutorial. I want to use this metode
Sep 30, 2010 @ 22:55:18
Goodam/pm can i import more than 50000 records in a csv with this fuction “LOAD DATA LOCAL INFILE”? i`d really appreciate if someone reply to me, thanks…
Sep 30, 2010 @ 22:56:03
What is the exact code to importing 5000 file in mysql? thanks
Nov 03, 2010 @ 15:23:02
Thankyou for your the information.
Nov 17, 2010 @ 13:07:44
It helped me a lot
shankar
Nov 27, 2010 @ 15:32:55
Updated post with link to some useful Excel export macros in vba: http://www.mcgimpsey.com/excel/textfiles.html
Dec 15, 2010 @ 18:46:42
in saving the excel file there are 3 name ending with .csv which to uese
Dec 15, 2010 @ 18:48:44
TYABO, you can use any of them depending on what separator you want to use. You then need to specify the separator when you import as described in post.
Dec 28, 2010 @ 09:04:43
Thanks for a really useful, practical post!
Mar 04, 2011 @ 16:40:41
How can I use this command to import a multivalue field from excel to mysql? The excel field is made of numbers with a comma between (i.e. 1,5,6).
Apr 28, 2011 @ 06:37:46
Here is what I use:
LOAD DATA INFILE ‘C:/FILE.CSV INTO TABLE table_name FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\n’ ;
May 07, 2011 @ 16:22:41
thank you very much,
it was very helpfull for me…
Jul 07, 2011 @ 23:41:00
HI
I am trying load csv files which has around 60,000 records into mysql , when I ran the query as mentioned in your post ,it seems like I am missing most of the records(around 50000 records).what do you think can be the reason for this?
Jul 21, 2011 @ 16:17:58
THANK YOU VERY MUCH..It is perfectly working for my table
Aug 18, 2011 @ 18:27:32
Please can some one help me out? i am to export a 2007 excel csv file to mysql 4.0 n3.2 using wamp server it will start importing but later on the server will stop working an error messege will be displayed.
can some one please help me out?
Aug 23, 2011 @ 05:54:36
Thanks,
I use this command and successfully transfer my 538 lines of excel sheet into table.
excellent command.
Sep 12, 2011 @ 10:42:28
how to import datetime from excel to mysql?
cause in mysql the result 0000000.. please help me.
Sep 14, 2011 @ 12:33:49
Many Thanks!!
That helped mV,
Sep 19, 2011 @ 16:18:35
En mi caso particular:
LINES TERMINATED BY ‘\r\n’
Debí eliminar esto pues solo insertaba la primera línea.
Además al hacerlo desde Linux:
LOAD DATA LOCAL INFILE ‘/home/mortiz/Escritorio/archivo_ejemplo.csv’
No se aceptan abreviaciones del tipo ~/, es decir nada de path relativo, toca escribir toda la ruta.
Gracias y muy útil.
Oct 05, 2011 @ 06:58:17
hi all
is there any one who could tell me how to fetch data from multiple sheets in excel as
i have total 5 sheets and each sheet have different data. what should i do
Oct 05, 2011 @ 17:34:05
am trying to import a csv file into mysql every time i attempt the server will generate an error :
Error
SQL query:
LOAD DATA INFILE ‘c:/orphanrecorddb.csv’ INTO TABLE orphanrecorddb FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’(
id, fID, fStatus, pArea, photo, oName, oSex, phyStatus, oDOB, bPlace, resAdd, oLGA, oState, oNation, rDate, rLocation, rReason, referee, relation, refAdd, refPNO, refEdu, oNeeds, needEdu, needSubs, needMed, needShelt, fName, fHome, fLGA, fState, fNation, fOccup, mName, mHome, mLGA, mState, mNation, mOccup, dataOfficer, cDate
)
MySQL said:
#1062 – Duplicate entry ’0′ for key 1
Every one please to help me out?
Oct 10, 2011 @ 15:31:45
Usman – see my last point about autoincrement, I think this may be your problem. Either don’t import the col that has autoincrement or alter the column in db before import to turn off autoincrement. Then import and turn autoincrement on again.
Hope this helps.
Oct 13, 2011 @ 12:39:05
Hi,
I need a script for querying mysql database and read the data from it and write it into a excel file on daily basis.. For example, No of users joined, no of files uploaded and downloaded.. I need to update all these informations into an excel sheet daily..
Can anyone help to solve it…?
Oct 29, 2011 @ 11:56:57
thanks alot, very good tutorial
Nov 09, 2011 @ 13:12:51
Thanks for this post. I had non-ASCII data to import. So I followed John Larsen’s method (sort of).
- imported the CSV into Access, specifying UTF-8 as the encoding of the input data.
- saved the Access db as Access 2003 (*.mdb), so that MySQL Migration Toolkit would be able to read it.
- installed the deprecated MySQL Migration Toolkit (http://dev.mysql.com/downloads/gui-tools/5.0.html), since there is no replacement for it yet in MySQL Workbench
- used the Migration Toolkit to import the mdb. Success! Special characters were imported correctly.
Nov 09, 2011 @ 13:26:00
Lars – I think you should be able to do this without having to use access and the migration toolkit.
Make sure the db is using the same character set (UTF-8) as your file and also you may need to specify character set by adding it to LOAD DATA, for example:
LOAD DATA LOCAL INFILE ‘C:\\temp\\yourfile.csv’ INTO TABLE database.table CHARACTER SET UTF-8 FIELDS TERMINATED BY ‘;’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\r\n’ (field1, field2);
More info in the reference manual http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Have not tested it yet so if someone does please confirm if it works or not.
Nov 16, 2011 @ 17:32:26
i tried it and got this error. please help
938 row(s) affected, 64 warning(s):
1265 Data truncated for column ‘Address’ at row 1
1261 Row 1 doesn’t contain data for all columns.
thanks.
Dec 24, 2011 @ 13:01:57
thanks for the post,
but how to import only for particular column in table…