Home > Database > Mysql Tutorial > mysql批量导入已经格式好的文本数据_MySQL

mysql批量导入已经格式好的文本数据_MySQL

WBOY
Release: 2016-06-01 13:01:13
Original
1105 people have browsed it

语法:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [FIELDS
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]
Copy after login

导入表中的某一列数据

数据格式:
北京
上海
深圳
广州
长沙
郑州
合肥
西安
南昌
南京
杭州
成都
Copy after login

sql代码:
LOAD DATA LOCAL INFILE 'C:\\Users\\XXX\\Desktop\\code.txt'
INTO TABLE sys_sensitiveword_t(cityname);
Copy after login

导入整个表数据

创建stu表:
create table stu_other(id int auto_increment primary key,stu_name varchar(50),age int);
Copy after login
然后创建一个文本文件“stu.txt”,每行包含一个记录,放在C盘的根目录下。默认的字段分隔符是(tab),你可以更改,下面说明。并且以CREATE TABLE语句中列出的列次序给出。对于丢失的值(例如未知的性别,或仍然活着的动物的死亡日期),你可以使用NULL值。为了在你的文本文件中表示这些内容,使用/N(反斜线,字母N)。 如stu.txt的内容如下:
100001    FLB100001     100001
100002    FLB100002     100002
100003    FLB100003     100003
100004    FLB100004     100004
100005    FLB100005     100005
100006    FLB100006     100006
100007    FLB100007     100007
100008    FLB100008     100008
Copy after login

这里我用的是table分开。 要想将文本文件“stu.txt”装载到stu表中,使用这个命令:
LOAD DATA LOCAL INFILE 'c://stu.txt' INTO TABLE stu;
Copy after login
这里没有指定字段分隔符,以及行的终止符,默认情况下是这样的: 字段分隔符:
<pre class="prebrush">FIELDS TERMINATED BY &#39;/t&#39; ENCLOSED BY &#39;&#39; ESCAPED BY &#39;//&#39;
Copy after login
其中的'/t'就表示是字段分隔符为tab键,如果不想更改,那就把FIELDS TERMINATED BY '/t'中的'/t'改在你自己的分隔符。将执行命令改成这样:
Copy after login
LOAD DATA LOCAL INFILE &#39;c:/stu.txt&#39; INTO TABLE stu fields terminated by ‘ ‘;
Copy after login
这里我用的是空格。 行终止符:
<pre class="prebrush">LINES TERMINATED BY &#39;/n&#39; STARTING BY &#39;&#39;
Copy after login
请注意如果用Windows中的编辑器(使用/r/n做为行的结束符)创建文件,应使用:
LOAD DATA LOCAL INFILE &#39;C://stu.txt&#39; INTO TABLE stu LINES TERMINATED BY &#39;/r/n&#39;;
Copy after login
(在运行OS X的Apple机上,应使用行结束符'/r'。) 如果你愿意,你能明确地在LOAD DATA语句中指出列值的分隔符和行尾标记,但是默认标记是定位符和换行符。这对读入文件“stu.txt”的语句已经足够。

导出表数据

select * from stu_t into outfile "c://stu_t.txt";
Copy after login
不过,没有按记录自动分行 这样才能够换行:
select * from stu_t into outfile "c://stu_t.txt" lines terminated by &#39;/r/n&#39;;
Copy after login




Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template