尝试使用下面的命令导入文本文件

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

表结构
id   INT Autoincremented
name Varchar
type Varchar
unit Varchar
crated datetime

但上面的命令是为name字段添加空白值。
这是我的示例文本文件:
American River College
American University
Amherst College

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

最佳答案

必须在()子句之前指定从SET中包围的文本文件直接映射的列的列表:

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";

考虑到你的输入数据,这是测试和我的开发数据库工作。
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 |
+----+------------------------+---------+------+---------------------+

文档中有点模棱两可:
...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

关于mysql - LOAD DATA LOCAL INFILE插入空白记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28543088/

10-13 03:34