[MySql]使用LOAD DATA导入数据

LOAD DATA导入数据语法:

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,...]

  

FIELDS和LINES都是可选的,但是如果都指定了,FIELDS必须在LINES之前。

本地用一个pet表,结构如下:

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

  

要导入的数据:pet.txt

Fluffy,Harold,cat,f,1993-02-04
Claws,Gwen,cat,m,1994-03-17
Buffy,Harold,dog,f,1989-05-13
Fang,Benny,dog,m,1990-08-27
Bowser,Diane,dog,m,1979-08-31,1995-07-29
Chirpy,Gwen,bird,f,1998-09-11
Whistler,Gwen,bird, ,1997-12-09
Slim,Benny,snake,m,1996-04-29

  

导入命令:

LOAD DATA LOCAL INFILE ‘/home/scott/MySql/pet.txt‘ IGNORE INTO TABLE pet fields TERMINATED BY ‘,‘ lines TERMINATED BY ‘\n‘;

  

导入结果:

mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    |      | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+

  

时间: 2024-10-31 04:00:41

[MySql]使用LOAD DATA导入数据的相关文章

mysql 字符集更改与导入数据

mysql 字符集更改与导入数据 mysqldb经常有中文乱码的问题,解决起来很恼火.其实所有开发和数据库统一为一种编码就可以了: utf8. 1 下面修改mysql的编码 1) 永久修改. 在/etc/mysql/my.cnf中添加下面二行: [client] ... default-character-set=utf8 ... [mysqld] ... character-set-server=utf8 ... 重启服务,然后登录看看: $ sudo /etc/init.d/mysql re

mysql中load data Infile运用

速度比insert要快20倍.共享一下java程序操作. package com.mysql.csv; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class Competition { static Connection conn = null; static String dr

mysql:执行LOAD DATA LOCAL 报错

mysql:执行LOAD DATA LOCAL 报错. 我使用navicat for mysql 连接 mysql服务器执行local 可以正常执行 . show VARIABLES like  '%local%infile%'结果如下:+---------------+-------+| Variable_name | Value |+---------------+-------+| local_infile  | ON    | +---------------+-------+1 row

MySQL用Load Data local infile 导入部分数据后中文乱码

今天在两台MySQL服务器之间导数据,因为另一个MySQL服务器是测试用的,差一个月的数据,从现有MySQL服务器select到一个文件,具体语句是: select * from news where ine_time>="2010-02-01"  and ine_time <"2010-03-01" into outfile "/tmp/newsdata.sql"; 然后scp到另一个MySQL 在线扑克室 Server上导入到对应

MySQL 之 LOAD DATA INFILE 快速导入数据

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]

MySQL通过 LOAD DATA INFILE 批量导入数据

LOAD DATA INFILE 语句用法 参考手册 本文语句参数使用默认值 PHP:  TP框架环境 // 定义文件路径$file_path = 'LOAD_DATA_LOCAL_INFILE.txt'; set_time_limit(1000); $fhandler = fopen($file_path,'w'); if($fhandler == false){ // 文件打开失败 } $id = 7; $add_time = time(); // 向文件中写入数据 这里是写入 10万条 $

mysql的load data,高速将文本文件,插入数据库中

1语法 LOAD DATA [ LOW_PRIORITY | CONCURRENT ] [ LOCAL ] INFILE 'file_name.txt' [ REPLACE | IGNORE ] INTO TABLE tbl_name [ FIELDS [ TERMINATED BY 'string' ] [ [OPTIONALLY] ENCLOSED BY 'char' ] [ ESCAPED BY 'char'  ] ] [ LINES [ STARTING BY 'string' ] [

mysql数据库LOAD DATA INFILE Syntax

1.LOAD DATA INFILE用来把一个文本文件里的内容高速写入到MySQL表里,它和SELECT ... INTO FILE的操作是对应的,一个导入.一个导出.使用LOAD DATA INFILE需要MySQL对要导入的文件有读权限,其语法如下: LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_name [PARTITION (partit

JDBC使用MYSQL的LOAD DATA LOACAL INFILE和LOAD DATA INFILE

MYSQL的LOAD方法都必须建立在mysql服务允许使用该命令的情况下: 开启该命令的方法: 1.在实例对应的my.cnf(windows为my.ini)中添加一行local-infile=1(默认好像是开启的),改为0即关闭,LOAD方式禁用. 2.加参数local-infile=1(默认是开启的,除非my.cnf中配置了禁止使用load才这样启动)启动mysql,"/usr/local/mysql/bin/mysqld_safe --user=mysql --local-infile=1