1

Trying to import text file using below command

LOAD DATA LOCAL INFILE '/var/www/colleges.txt' 
INTO TABLE selections LINES TERMINATED BY '\n' 
SET unit=null, created=NOW(), type="college";

Table Structure

id   INT Autoincremented
name Varchar
type Varchar
unit Varchar
crated datetime

But the above command is adding blank value for name field.

Here is my sample text file:

American River College
American University
Amherst College

(http://india-webdev.com/demo/colleges.txt)

Neeraj
  • 8,625
  • 18
  • 60
  • 89

1 Answers1

1

You must specify the list of columns that are directly mapped from the text file surrounded in (), just before the SET clause:

LOAD DATA LOCAL INFILE '/var/www/colleges.txt' 
INTO TABLE selections 
LINES TERMINATED BY '\n' 
-- Here, list columns from the file in ()
(name)
-- Followed by columns for which you set values
SET
  unit=null,
  created=NOW(),
  type="college";

Given your input data, this is tested and working on my development database.

MariaDB [test]> select * from selections;
+----+------------------------+---------+------+---------------------+
| id | name                   | type    | unit | created             |
+----+------------------------+---------+------+---------------------+
|  1 | American River College | college | NULL | 2015-02-16 08:08:01 |
|  2 | American University    | college | NULL | 2015-02-16 08:08:01 |
|  3 | Amherst College        | college | NULL | 2015-02-16 08:08:01 |
+----+------------------------+---------+------+---------------------+

It is a little ambiguous in the documentation:

...snip...
[LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    -- HERE....
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390