1

I'm trying to import some data to database using: load data infile.

For example:

I have table (name: tab) with two columns: colA, colB.

In file there are three columns, example file content: value1|value2|value3

I want that value2 goes to colA and value3 goes to colB. I don't need value1.

In Mysql 5 I did it like this:

LOAD DATA INFILE <filename>
REPLACE
INTO TABLE tab
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\r'
(@dummy, colA, colB);

It works great but the problem is when I try do it in Mysql 4. There is an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@dummy, colA, colB)' at line 6

I checked in mysql documentation: http://dev.mysql.com/doc/refman/4.1/en/load-data.html and in mysql 4 values from file can't be save to user variable (in my example to @dummy).

Do you have any ideas how to import (using load data infile) only some columns from file in Mysql 4?

Thanks

2
  • 2
    Why on EARTH are you using MySQL 4? It's not 1990 anymore... Commented Dec 27, 2013 at 13:52
  • I'm not using. There is a very old application using it and i had to work with it. This is also very big application which is using by many customers and upgrade to mysql 5 is not so easy. :P Commented Sep 16, 2014 at 8:11

2 Answers 2

1

You could load the data into a temporary table and then use REPLACE ... SELECT:

CREATE TEMPORARY TABLE tmptab LIKE tab;
ALTER TABLE tmptab ADD COLUMN dummy TEXT;

LOAD DATA INFILE <filename>
  INTO TABLE tmptab
  FIELDS TERMINATED BY '|'
  LINES TERMINATED BY '\r'
  (dummy, colA, colB);

REPLACE INTO tmp (colA, colB) SELECT (colA, colB) FROM tmptab;

DROP TEMPORARY TABLE tmptab;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Thanks for answer. It is a good idea but this solve require to create another table, and i afroid that can be slow. Maybe there is a solution without creating temporary table ?
Finally i done it this way. It worked not so bad (although i had many files with many rekords). Thanks
0

A comment to the last answer(Since I am unable to comment):

If the code:

LOAD DATA INFILE <filename>
  INTO TABLE tmptab
  FIELDS TERMINATED BY '|'
  LINES TERMINATED BY '\r'
  (dummy, colA, colB);

-leads to some warning, then use:

LOAD DATA INFILE <filename>
  INTO TABLE tmptab
  FIELDS TERMINATED BY '|'
  LINES TERMINATED BY '\r\n'
  (@dummy, colA, colB);

Thank You.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.