1.select into outfile
1)创建表并插入数据
MariaDB [mt]> create table filetest(a int,b int,c int); Query OK, 0 rows affected (0.014 sec) MariaDB [mt]> insert into filetest values(1,2,3) -> ,(2,3,4),(3,4,5),(4,5,6),(5,6,7),(6,7,8); Query OK, 6 rows affected (0.005 sec) Records: 6 Duplicates: 0 Warnings: 0 MariaDB [mt]> select * from filetest; +------+------+------+ | a | b | c | +------+------+------+ | 1 | 2 | 3 | | 2 | 3 | 4 | | 3 | 4 | 5 | | 4 | 5 | 6 | | 5 | 6 | 7 | | 6 | 7 | 8 | +------+------+------+ 6 rows in set (0.000 sec)
2)导出:
语法:
SELECT ... INTO OUTFILE 'file_name' [CHARACTER SET charset_name] [export_options] export_options: [{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char'] ] [LINES [STARTING BY 'string'] [TERMINATED BY 'string']
]
说明:
FIELDS,如果用到这个关键字,MySQL剖析器希望看到至少有下面的一个选项:
terminated by描述字段的分隔符,默认情况下是tab字符( )
enclosed by描述的是字段的括起字符。
escaped by描述的转义字符。默认的是反斜杠(backslash: )
LINES 关键字指定了每条记录的分隔符默认为'
'即为换行符,如果用到这个关键字,MySQL剖析器希望看到至少有下面的一个选项:
terminated by 分隔符:意思是以什么字符作为一行结束
STARTING BY分隔符:意思是以什么字符作为一行开始
示例1:
SELECT * FROM filetest INTO OUTFILE 'E:/test/a.txt';
报错:
MariaDB [mt]> SELECT * FROM filetest INTO OUTFILE 'E: esta.txt';
ERROR 1290 (HY000): The MariaDB server is running with the --secure-file-priv option so it cannot execute this statement
MariaDB执行select into outfile和load data outfile,outfile应该在secure_file_priv全局变量指定的文件夹,这个全局变量必须在配置文件中进行配置
修改配置文件,[mysqld]部分最下面加如下配置
secure_file_priv=E:/test
重启MariaDB,然后重新导出:
ariaDB [mt]> SELECT * FROM filetest INTO OUTFILE 'E:/test/a.txt';
Query OK, 6 rows affected, 1 warning (0.004 sec)
导出成功,文件如下:
示例2:
SELECT * FROM filetest INTO OUTFILE 'E:/test/b.txt' FIELDS TERMINATED BY '&' LINES TERMINATED BY '#';
导出效果:
示例3:
SELECT * FROM filetest INTO OUTFILE 'E:/test/b.txt' FIELDS TERMINATED BY '&' LINES TERMINATED BY '# ';
导出效果:
2.load data infile
load data infile语句从一个文本文件中以很高的速度读入一个表中。为了安全原因,当读取位于服务器上的文本文件时,文件必须处于数据库目录或secure_file_priv指定目录。另外,为了对服务器上文件使用load data infile,在服务器主机上你必须有file的权限。
1)基本语法:
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,...]
[LOW_PRIORITY | CONCURRENT]:如果你指定关键词low_priority,那么MySQL将会等到没有其他人读这个表的时候,才把插入数据。可以使用如下的命令:load data low_priority infile "/home/mark/data sql" into table Orders;
[LOCAL]:如果指定local关键词,则表明从客户主机读文件。如果local没指定,文件必须位于服务器上。
[REPLACE | IGNORE]replace和ignore关键词控制对现有的唯一键记录的重复的处理。如果你指定replace,新行将代替有相同的唯一键值的现有行。如果你指定ignore,跳过有唯一键的现有行的重复行的输入。如果你不指定任何一个选项,当找到重复键时,出现一个错误,并且文本文件的余下部分被忽略。
FIELDS,如果用到这个关键字,MySQL剖析器希望看到至少有下面的一个选项:
terminated by描述字段的分隔符,默认情况下是tab字符( )
enclosed by描述的是字段的括起字符。
escaped by描述的转义字符。默认的是反斜杠(backslash: )
例如:load data infile "/home/mark/Orders txt" replace into table Orders fields terminated by',' enclosed by '"';
LINES 关键字指定了每条记录的分隔符默认为'
'即为换行符,如果用到这个关键字,MySQL剖析器希望看到至少有下面的一个选项:
terminated by 分隔符:意思是以什么字符作为一行结束
STARTING BY分隔符:意思是以什么字符作为一行开始
例如:load data infile "/jiaoben/load.txt" replace into table test fields terminated by ',' lines terminated by '/n';
2)关于灵活性,其实也就是一个记录功能
如果想在导入的时候记录一下导入的具体时间怎么办?
mysql> load data infile '/tmp/t0.txt' into table t0 character set gbk fields terminated by ',' enclosed by '"' lines terminated by ' ' (`name`,`age`,`description`) set update_time=current_timestamp;
示例:将前面的c.txt导回
mysql> load data infile 'E:/test/c.txt' into table filetest character set utf8 fields terminated by '&' lines terminated by '# ';