load data infile使用詳解

2021-06-28 16:44:20 字數 1822 閱讀 5101

基本語法:

load data  [low_priority] [local] infile 'file_name txt' [replace | ignore]

into

table tbl_name

[fields

[terminated by

't']

[optionally] enclosed by

''][escaped by

'\' ]]

[lines terminated by'n']

[ignore number lines]

[(col_name, )]

load data infile語句從乙個文字檔案中以很高的速度讀入乙個表中。使用這個命令之前,mysqld程序(服務)必須已經在執行。為了安全原因,當讀取位於伺服器上的文字檔案時,檔案必須處於資料庫目錄或可被所有人讀取。另外,為了對伺服器上檔案使用load data infile,在伺服器主機上你必須有file的許可權。

4 分隔符

(1) fields關鍵字指定了檔案記段的分割格式,如果用到這個關鍵字,mysql剖析器希望看到至少有下面的乙個選項:

terminated by分隔符:意思是以什麼字元作為分隔符

enclosed by字段括起字元

escaped by轉義字元

terminated by描述欄位的分隔符,預設情況下是tab字元(\t)

enclosed by描述的是字段的括起字元。

escaped by描述的轉義字元。預設的是反斜槓(backslash:\ )

例如:load data infile 「/home/mark/orders txt」 replace into table orders fields terminated by』,』 enclosed by 『」』;

(2)lines 關鍵字指定了每條記錄的分隔符預設為』\n』即為換行符

如果兩個欄位都指定了那fields必須在lines之前。如果不指定fields關鍵字預設值與如果你這樣寫的相同: fields terminated by』\t』 enclosed by 』 」 『 escaped by』\』

如果你不指定乙個lines子句,預設值與如果你這樣寫的相同: lines terminated by』\n』

例如:load data infile 「/jiaoben/load.txt」 replace into table test fields terminated by 『,』 lines terminated by 『/n』;

5 load data infile 可以按指定的列把檔案匯入到資料庫中。 當我們要把資料的一部分內容匯入的時候,,需要加入一些欄目(列/字段/field)到mysql資料庫中,以適應一些額外的需要。比方說,我們要從access資料庫公升級到mysql資料庫的時候

下面的例子顯示了如何向指定的欄目(field)中匯入資料:

load data infile 「/home/order txt」 into table orders(order_number, order_date, customer_id);

6 當在伺服器主機上尋找檔案時,伺服器使用下列規則:

(1)如果給出乙個絕對路徑名,伺服器使用該路徑名。

(2)如果給出乙個有乙個或多個前置部件的相對路徑名,伺服器相對伺服器的資料目錄搜尋檔案。

(3)如果給出乙個沒有前置部件的乙個檔名,伺服器在當前資料庫的資料庫目錄尋找檔案。

例如: /myfile txt」給出的檔案是從伺服器的資料目錄讀取,而作為「myfile txt」給出的乙個檔案是從當前資料庫的資料庫目錄下讀取。

load data infile 中文亂碼解決

系統配置linux mysql 預設utf8 首先使用show variables like char variable name value character set client gbk character set connection gbk character set database u...

LOAD DATA INFILE使用與詳解

mysql匯入資料一般都是用insert into,那麼mysql有沒有乙個高效匯入方法呢?答案是肯定的,那就是load data infile,下面我們來詳細的講解一下相關的使用方法 基本語法 根據官方的 load data low priority concurrent local infile...

LOAD DATA INFILE字元編碼的問題

quote load data infile c users denny desktop testpage xx.csv into table taobao shop character set utf8 fields terminated by enclosed by escaped by lin...