找回密码
 注册

QQ登录

只需一步,快速开始

查看: 305|回复: 0

5 Easy Steps Importing Excel Data into MySQL

[复制链接]
发表于 2013-5-14 14:34:01 | 显示全部楼层 |阅读模式
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]


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-9 22:22 , Processed in 0.016000 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表