Saturday, 10 January 2015

PHPmyAdmin importing csv file to MySql database

This is pretty easy once you work out a couple of the idiosyncrasies involved in the process. These are my tips to help if you are experiencing difficulties.
1. When you develop your excel file save it out as a csv file. This means do not save it as a work book or any other format. When it has been saved take a look at it with a text reader such as notepad or similar. You will probably notice that each column is separated by a comma (,). This is important when we look at importing the file with PHPmyAdmin.
2. Cleanse your CSV file. Get rid of column headings. Ensure that there is an entry of some form in each row and column. Get rid of blank areas.
3. You must ensure that the table you are importing your data into is represented identically in the CSV file. This means that if your table has 10 columns you must have 10 corresponding columns in your CSV file. There is one exception to this however, if your first column is an id autoincrement column this does not need to be one of the included columns in your CSV. For example if you have a table structure like this:
MySql Table: Competitors
id,eventID,imageID,raceNumber,firstName,lastName,raceTime,category,
catPosition,gender,cap
CSV File:
eventID,imageID,raceNumber,firstName,lastName,raceTime,category,
catPosition,gender,cap
The id column is not represented in the CSV file.
4. If you do not have data for some of the columns in the CSV file enter 0 for each line.
5. In PHPmyAdmin click on the import tab. Click on the browse button and select your csv file. It will automatically select the CSV in Format or Imported File area.
6. In the Format of Imported File area:
Fields terminated by ,
Fields enclosed by (leave blank)
Fields escaped by
Lines terminated by auto
Column names eventID,imageID,raceNumber,firstName,lastName,raceTime,category,
catPosition,gender,cap (this is the example above used again here)
7. The category names above are separated by a comma (,) and there are no spaces after or before each word.
8. Click on Go
If everything went well you should have a fully populated table.

No comments:

Post a Comment