MySQL how to specify string position with LOAD DATA INFILE

后端 未结 3 519
抹茶落季
抹茶落季 2021-01-28 14:20

I have ASCII files with a static number of characters for each line with no delimiters. I\'d like to use LOAD DATA INFILE to import into my table.

Example of file:

3条回答
  •  别那么骄傲
    2021-01-28 15:00

    According to the documentation, you can load a fixed format file without using a temporary table.

    If the FIELDS TERMINATED BY and FIELDS ENCLOSED BY values are both empty (''), a fixed-row (nondelimited) format is used. With fixed-row format, no delimiters are used between fields (but you can still have a line terminator). Instead, column values are read and written using a field width wide enough to hold all values in the field. For TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT, the field widths are 4, 6, 8, 11, and 20, respectively, no matter what the declared display width is.

    The positions are derived from the columns definitions, which in your case match the structure of the file. So you just need to do:

    LOAD DATA INFILE 'your_file' INTO TABLE your_table
      FIELDS TERMINATED BY ''
      LINES TERMINATED BY '\r\n'
      SET name = trim(name);
    

提交回复
热议问题