LOAD DATA LOCAL INFILE 插入空白记录
LOAD DATA LOCAL INFILE inserting blank record
正在尝试使用以下命令导入文本文件
LOAD DATA LOCAL INFILE '/var/www/colleges.txt'
INTO TABLE selections LINES TERMINATED BY '\n'
SET unit=null, created=NOW(), type="college";
Table结构
id INT Autoincremented
name Varchar
type Varchar
unit Varchar
crated datetime
但是上面的命令为 name
字段添加了空白值。
这是我的示例文本文件:
American River College
American University
Amherst College
您必须在 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,...]
正在尝试使用以下命令导入文本文件
LOAD DATA LOCAL INFILE '/var/www/colleges.txt'
INTO TABLE selections LINES TERMINATED BY '\n'
SET unit=null, created=NOW(), type="college";
Table结构
id INT Autoincremented
name Varchar
type Varchar
unit Varchar
crated datetime
但是上面的命令为 name
字段添加了空白值。
这是我的示例文本文件:
American River College
American University
Amherst College
您必须在 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,...]