Wednesday, April 02, 2008

Importing MS/Win Excel Data Into MySQL on Mac OS X

I've got to write this down somewhere. It seems like every-time I have to do this I waste an hour trying to make this work.

Here are the steps for exporting MS Win/Excel data into a MySQL database hosted on Mac OS 10.5

  1. Save the Excel data as a tab delimited file, and move the file to the Mac OS (for some reason I've found more consitant success starting from a tab delimited file than with a .csv.

  2. Open the Excel tab delimited file in Numbers spreadsheet program and export as CSV. Doing this seems to work out any WIN/Mac file formatting errors.

  3. Run the following command at the terminal in the Mac OS

    load data local infile
    -> '~/filename.csv' into table tablename
    -> fields terminated by ','
    -> lines terminated by '\n'
    -> (field_1, field_2, field_n);
  4. The statement above assumes your data is separated by comas, and lines are terminated by the newline character; field_1, field_2, field_n are positional -- that is, whatever column comes first in filename.csv will be mapped to the field_1, whatever comes second in filename.csv will be mapped to field_2 etc...




Never look this up again.

No comments: