Forum Moderators: open

Message Too Old, No Replies

MySQL, LOAD DATA LOCAL INFILE using fixed width data

         

csdude55

5:05 am on Jan 5, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a text file, named data.dat. The data doesn't have delimiters, but each column has a fixed width.

I'm trying to upload it to MySQL using:

LOAD DATA LOCAL INFILE 'data.dat' INTO TABLE table


It's my understanding that the default here is fixed-width, based on the length defined in the table structure.

My table looks like:

col1 char(7)
col2 char(10)
col3 char(30)
col4 char(10)
col5 char(30)
col6 char(10)
col7 char(8)

They all have the default collation of "latin1_swedish_ci" (which I didn't set, so I guess that's the default).

The problem I'm having is that when I import the data, col1 comes in right but the rest of the data is ignored. So col2 - col7 are all empty.

Am I wrong about the default being fixed width? If so, how do I make it load correctly?

In researching, I found this alternative:


LOAD DATA LOCAL INFILE 'data.dat' INTO TABLE table
(@var1)
SET
col1 = SUBSTR(@var1, 1, 7),
col2 = SUBSTR(@var1, 8, 10),
col3 = SUBSTR(@var1, 18, 30),
col4 = SUBSTR(@var1, 48, 10),
col5 = SUBSTR(@var1, 58, 30),
col6 = SUBSTR(@var1, 88, 10),
col7 = SUBSTR(@var1, 98, 8);


But in that, I have no idea what @var1 represents? Or is it just a placeholder that I should leave named as @var1?

csdude55

10:28 am on Jan 5, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For future readers, I just now ran the second code, and it worked exactly as expected. So @var1 does appear to be a generic placeholder that could presumably be anything, as long as you use the same variable name throughout the query.

So that works, but I'm still not sure why it didn't recognize fixed width without setting each of them manually.