Loading fixed-width, space delimited .txt file into mySQL

前端 未结 2 1037
旧时难觅i
旧时难觅i 2020-12-17 00:43

I have a .txt file that has a bunch of formatted data in it that looks like the following:

...
   1     75175.18     95128.46
   1    790890.89    795829.16
         


        
相关标签:
2条回答
  • 2020-12-17 01:18
        LOAD DATA
    CHARACTERSET AL32UTF8
    INFILE 'DCF Master 14APR2013 VSPCFM_reduced size.txt'
    INTO TABLE EMPLOYEE3
    (
    a = TRIM(SUBSTR(@row,1,11)),
    b = TRIM(SUBSTR(@row,33,38)),
    c = TRIM(SUBSTR(@row,70,86))
    )
    
    0 讨论(0)
  • 2020-12-17 01:28

    These are what we call "fixed-width" records and LOAD DATA doesn't play well with them. Options:

    1. Clean up data in Excel first, or
    2. Load up the data to a temp table with only 1 column, shoving an entire text row into that column. Then you can use SUBSTR() and TRIM() to slice out the columns you need into the final table.
    3. Or with user variables (@row) you can do it all within the LOAD DATA statement.
    LOAD DATA LOCAL INFILE 
    '/some/Path/segmentation.txt' 
    INTO TABLE clip
    (@row)
    SET slideNum = TRIM(SUBSTR(@row,1,4)),
        startTime = TRIM(SUBSTR(@row,5,13)),
        endTime = TRIM(SUBSTR(@row,18,13))
    ;
    
    0 讨论(0)
提交回复
热议问题