phpmysqlcsvfastercsv

remove first row from csv file and create table according to it


Hi I am trying to be at point.

I am trying to upload CSV into mysql atonce by using php in windows. so I am using the query

LOAD DATA INFILE 'c:/Data/mysql/transaction_upload.csv'
INTO TABLE transaction_upload
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n';

it load my whole csv into the table.

The problem is:

  1. The condition is that the table "transaction_upload" should be already exist.
  2. It upload first row of csv too which contain header.

I want that first row will removed from CSV i.e. the header should not be have entry in DB and the table created automatically having name as CSV_file_name and columns as CSV first row's columns.

Please do not answer as read first line of CSV in php and create table because the problem of removing first row from is still there.


Solution

  • Add IGNORE 1 LINES to the end of your LOAD DATA command.

    As the manual says:

    The IGNOREnumberLINES option can be used to ignore lines at the start of the file. For example, you can use IGNORE 1 LINES to skip over an initial header line containing column names:

    LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES;

    If the table does not already exist, MySQL will raise an error.