mysql導入導出數據—loaddata的用法


 

一、loaddata 導入數據的語法 load data infile

基本語法

load data [low_priority] [local] infile 'file_name.txt' [replace|ignore]
into table table_name
fields
[terminated by '\t']
[[OPTIONALLY] enclosed by '']
[escaped by '\' ]
[lines terminated by '\n']
[ignore number lines]
[(col_name,   )]

參數說明

     參     數                   說    明          備    注 
low_priority MySQL將會等到沒有其他人讀這個表的時候,才把數據插入 load data low_priority infile "/tmp/harara.sql" into table orders;
local 表明從客戶主機讀文件。如果local沒指定,文件必須位於服務器上 load data low_priority local infile "/tmp/harara.sql" into table orders;
 replace 如果你指定replace,新行將代替有相同的唯一健值的現有行 load data low_priority local infile "/tmp/harara.sql" replace into table orders;
 ignore 如果你指定ignore,跳過有唯一鍵的現有行的重復行輸入  load data low_priority local infile "/tmp/harara.sql" ignore into table orders;
 fields terminated by  描述字段的分隔符,默認情況下是tab字符(\t) load data infile "/tmp/harara.sql" replace into table orders fields terminated by ',' enclosed by '"' escaped by '\';
 fields enclosed by 描述的是字段的括起字符。
 fields escaped by  escaped by 描述的轉義字符。默認的是反斜杠: \ 
 lines terminated by'\n' 指定每條記錄的分隔符默認為"\n" 即為換行符 load data infile "/tmp/harara.sql" replace into table test fields terminated by ',' lines terminated by '\n';
 (col_name,    ) 可以按指定的列把文件導入到數據庫中

當我們要把數據的一部分內容導入,需要加入一些欄目(列/字段/field)到MySQL數據庫中,以適應一些額外的需要。

比如,我們要從Access數據庫升級到MySQL數據庫的時候,下面的例子顯示了如何向指定的欄目(field)中導入數據:

load data infile "/home/harara.sql" into table orders(order_number,order_date,customer_id)

 

導入數據完整示例

load data local infile '/var/lib/mysql/emp8/customers.txt' replace into table lf_lanx_tmplp1 character set gbk fields terminated by ',' enclosed by '"' lines terminated by '\n' (TMPl_NAME);

 

 

二、loaddata 導出數據的語法 select into outfile

基本語法

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 terminated by 'str' 設置字段之間的分隔符,默認是"\t"
fields  enclosed by 'char' 設置包括住字段的值的符號,如單引號、雙引號等,默認情況下不使用任何符號
fields optionally enclosed by 'char' 設置括住CHAR、VARCHAR和TEXT等字符型字段的分隔符,默認情況下不使用任何符號
fields escaped by 'char' 設置轉義字符,默認值為"\"
lines starting by 'str' 設置每行數據開頭的字符,可以為單個或多個字符。默認情況下不使用任何字符
lines terminated by 'char' 設置每行數據結尾的字符,可以為單個或多個字符。默認值是"\n"

 

完整實例

SELECT TMPl_NAME INTO OUTFILE 'u/customers.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM LF_LANX_TMPLP1;

 

 

參考地址:

mybatis 處理大批量數據。使用mysql的LOAD DATA INFILE : https://blog.csdn.net/xcc_2269861428/article/details/83861665

mysql高效導入導出load data [infile][outfile]用法 : https://www.cnblogs.com/yyy-blog/p/11073855.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM