Search code examples
mysqlload-data-infile

MySQL ignoring fields in a csv file after LOAD DATA statement


I am loading a local csv for a table in MySQL. The table has 6 columns (or fields), but, after I load the data, only the message_date column is filled. In other words, the other 5 columns in the table are all NULL, after the LOAD DATA run. Why? I need to SET all of the 5 columns to bring them to my table?

Detail: When I do not SET any variable, just import them all as character fields, all of the columns appears. So it looks like is something after the IGNORE statement that is affecting all of the other 5 columns.

LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/DadosBrutosEventTracks.csv'
    INTO TABLE messages
    FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n'
    IGNORE 1 LINES
    (@var1, @var2, @var3, @var4, @var5, @var6)
    SET 
    message_date = STR_TO_DATE(SUBSTRING(@var3, 1, 22), "%Y-%m-%d %H:%i:%s.%f");

Solution

  • You only load data into variables. Therefore no data goes into any columns. Loading data into variables happens instead of loading into columns.

    (@var1, @var2, @var3, @var4, @var5, @var6)
    

    Then you set one column:

    SET 
    message_date = STR_TO_DATE(SUBSTRING(@var3, 1, 22), "%Y-%m-%d %H:%i:%s.%f");
    

    The other columns are not mentioned in the SET clause, so they get no data.