圖示
語法
跟普通的select查詢語句基本一致,只不過是指定了輸出地址用於備份文件內容。
select * into outfile '文件保存地址/文件名' from 表名;
實際操作
在操作時可能會出現The MySQL server is running with the --secure-file-priv option so it cannot execute this statement。這是因爲安裝時my.ini中secure-file-priv的設置限制了導出導入的目錄。可以在客戶端輸入以下命令查看:
show variables like '%secure%';
此時顯示的secure_file_priv對應的value就是導出目錄,如果對應的值爲NULL,說明my.ini文件沒有配置這個選項,就需要配置一下;如果有目錄就可以就文件導入到該目錄下。當然也可以修改my.ini使得導出目錄爲任意。
打開my.ini,輸入以下內容:
然後以管理員身份打開cmd,關閉mysql服務器,再打開實現重啓服務器就好了。
net stop mysql
net start mysql
最後查詢效果如下:
這樣就能愉快的使用了。
最終效果如下:
細節
-
可以自動創建文件,但是不能重寫已有文件。
-
mysql保存的文件格式(默認):
從以上圖可知字段與字段之間是以製表符分隔,每個記錄以換行符分隔。但是爲了滿足某種特別的需求,會採用不同的分割方式。可以在導出數據時,設置記錄,與字段的分隔符。
可以通過如下選項:fields:設置字段選項。 默認:fields terminated by '\t' enclosed by ' ' escaped by '\\' 字段結束符、包裹符、轉義字符,轉義字符一般使用默認的 Lines:設置行選項(記錄選項)。 默認:lines terminated by '\n' starting by ' 行結束符、行起始符
語句:
select * into outfile '文件保存地址/文件名'
fields terminated by ','
lines terminated by '\n' starting 'start:'
from 表名;
舉例:
select * into outfile 'e:/auto_int33'
-> fields terminated by ','
-> lines terminated by '\n' starting by 'start:'
-> from auto_int3;
select * into outfile 'e:/auto_int34'
-> fields terminated by '\t' enclosed by 'x'
-> lines terminated by '\n' starting by 'start:'
-> from auto_int3;
- 常規的,所有記錄應該通過行來顯示。例外的是保存二進制數據,此時不能使用outfile ,應該使用dumpfile。作用與outfile一致,只不過不做任何換行和轉義處理,非常適合導出二進制文件。