码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 之 LOAD DATA INFILE 快速导入数据

时间:2017-11-06 21:13:08      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:报错   current   offset   div   nta   cond   rip   local   expr   

 

SELECT INTO OUTFILE

> help select;
Name: SELECT
Description:
Syntax:
SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [MAX_STATEMENT_TIME = N]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
      [PARTITION partition_list]
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE ‘file_name‘
        [CHARACTER SET charset_name]
        export_options
      | INTO DUMPFILE file_name
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

举例:

> select * from e;
+------+-------+-------+
| id   | fname | lname |
+------+-------+-------+
| 1669 | Jim   | Smith |
|  337 | Mary  | Jones |
| 2005 | Linda | Black |
+------+-------+-------+

> select * from e into outfile "/data/mysql/e.sql";
或 > select * into outfile "/data/mysql/e.sql" from e; # cat e.sql
1669 Jim Smith 337 Mary Jones 2005 Linda Black

可以看到,select...into outfile 的结果只包含了表数据,默认以 Tab 分隔,也可指定分隔符:

> select * from e into outfile "/data/mysql/e.sql" fields terminated by ,;
# cat  e.sql    
1669,Jim,Smith
337,Mary,Jones
2005,Linda,Black

注意:outfile ‘/path/file‘,中的 path 需要有mysql的权限,否则会报错:

> select * from t into outfile "/root/backup/mysql/t.sql";
ERROR 1 (HY000): Cant create/write to file /root/backup/mysql/t.sql (Errcode: 13 - Permission denied)

 

LOAD DATA INFILE

LOAD DATA INFILE 语句以非常高的速度从文本文件中读取行到表中。

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE file_name
[REPLACE | IGNORE]
INTO TABLE tbl_name
[PARTITION (partition_name,...)]
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY ‘string‘]
[[OPTIONALLY] ENCLOSED BY char]
[ESCAPED BY ‘char‘]
]
[LINES
[STARTING BY ‘string‘]
[TERMINATED BY ‘string‘]
]
[IGNORE number {LINES | ROWS}]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]

举例:

> delete from e;
>
load data infile "/data/mysql/e.sql" into table e fields terminated by ,; Query OK, 3 rows affected (0.01 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0 > select * from e; +------+-------+-------+ | id | fname | lname | +------+-------+-------+ | 1669 | Jim | Smith | | 337 | Mary | Jones | | 2005 | Linda | Black | +------+-------+-------+

因为我们前面指定的分隔符是 ‘,‘,load data 时也要指定分隔符,否则也会报错:

> load data infile "/data/mysql/e.sql" into table e;
ERROR 1265 (01000): Data truncated for column id at row 1

 

如果数据被某种符号封闭着,需要指定 ‘ fields enclosed by ’ :

# cat e.sql 
"1669"  "Jim"   "Smith"

 > load data infile "/data/mysql/e.sql" into table e;
 ERROR 1366 (HY000): Incorrect integer value: ‘"1669"‘ for column ‘id‘ at row 1


 > load data infile "/data/mysql/e.sql" into table e fields enclosed by ‘"‘;
 Query OK, 1 row affected (0.01 sec)
 Records: 1 Deleted: 0 Skipped: 0 Warnings: 0

如上所示,数据被双引号封闭着,如果直接执行,会报错。

另外还有一些格式上的限制,如 LINES TERMINATED BY ‘string‘ ,指定 file 的换行符,如 ‘\n’ 。

 

MySQL 之 LOAD DATA INFILE 快速导入数据

标签:报错   current   offset   div   nta   cond   rip   local   expr   

原文地址:http://www.cnblogs.com/waynechou/p/7794939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!