mysql 匯入資料夾 MySQL 匯入與匯出

2021-10-17 16:54:29 字數 4127 閱讀 2452

csv檔案匯入mysql

load data infile語句允許您從文字檔案讀取資料,並將檔案的資料快速匯入資料庫的表中。

匯入檔案操作之前,需要準備以下內容:

一、將要匯入檔案的資料對應的資料庫表。

二、準備好乙個csv檔案,其資料與表的列數和每列中的資料型別相匹配。

三、連線到mysql資料庫伺服器的帳戶具有file和insert許可權。

首先,建立discounts表:

use testdb;

create table discounts (

id int not null auto_increment,

title varchar(255) not null,

expired_date date not null,

amount decimal(10 , 2) null,

primary key (id)

discounts.csv檔案的內容,第一行作為列標題和其他三行則為資料。

id,title,expired date,amout1,"spring break 2018",20180401,20

2,"back to scholl 2017",20170901,29

3,"summer 2018",20180820,100

接下來,將資料從f:/worksp/mysql/discounts.csv檔案匯入到discounts表。

load data infile 'f:/worksp/mysql/discounts.csv'into table discounts

fields terminated by','enclosed by'"'lines terminated by'\n'ignore1 rows;

檔案的字段由field terminated by ','指示的逗號終止,並由enclosed by '"'指定的雙引號括起來。

因為檔案第一行包含列標題,列標題不需要匯入到表中,因此通過指定ignore 1 rows選項來忽略第一行。

匯入時轉換資料

資料格式與表中的目標列不匹配,用load data infile語句中的set子句進行轉換。

有乙個discount_2.csv檔案中,它儲存的過期日期列是mm/dd/yyyy格式。內容如下:

id,title,expired date,amout4,"item-4","01/04/2018",200

5,"item-5","01/09/2017",290

6,"item-6","12/08/2018",122

將資料匯入discounts表時,必須使用str_to_date()函式將其轉換為mysql日期格式

load data infile 'f:/worksp/mysql/discounts_2.csv'

into table discounts

fields terminated by ',' enclosed by '"'

lines terminated by '\n'

ignore 1 rows

(id,title,@expired_date,amount)

set expired_date = str_to_date(@expired_date, '%m/%d/%y');

將檔案從客戶端匯入遠端mysql資料庫伺服器

使用load data infile語句將資料從客戶端(本地計算機)匯入遠端mysql資料庫伺服器。

load data infile中使用local選項時,客戶端程式會讀取客戶端上的檔案並將其傳送到mysql伺服器。該檔案將被上傳到資料庫伺服器作業系統的臨時資料夾,

windows上的c:\windows\temp,此資料夾不可由mysql配置或確定。

示例:load data local infile'c:/tmp/discounts.csv'into table discounts

fields terminated by','enclosed by'"'lines terminated by'\n'ignore1 rows;

如果載入乙個大的csv檔案,將會看到使用local選項來載入該檔案將會稍微慢些,因為需要時間將檔案傳輸到資料庫伺服器。

使用local選項時,連線到mysql伺服器的帳戶不需要具有file許可權來匯入檔案。

mysql匯出csv檔案

匯出資料之前,必須確保:

一、mysql伺服器的程序對包含目標csv檔案的目標資料夾具有寫訪問許可權。

二、要匯出的目標csv檔案不能存在。

示例:查詢從orders表中查詢選擇已取消的訂單,將此結果集匯出為csv檔案

select

ordernumber, status, orderdate, requireddate, comments

from

orders

where

status= 'cancelled'into outfile'f:/worksp/mysql/cancelled_orders.csv'fields enclosed by'"'terminated by';'escaped by'"'lines terminated by'\r\n';

該語句在f:/worksp/mysql/目錄下建立乙個包含結果集,名稱為cancelled_orders.csv的csv檔案。

csv檔案包含結果集中的行集合。每行由乙個回車序列和由lines terminated by '\r\n'子句指定的換行字元終止。檔案中的每行包含表的結果集的每一行記錄。

每個值由fields enclosed by '"'子句指示的雙引號括起來。 這樣可以防止可能包含逗號(,)的值被解釋為字段分隔符。 當用雙引號括住這些值時,該值中的逗號不會被識別為字段分隔符。

將資料匯出到檔名包含時間戳的csv檔案

將資料匯出到csv檔案中,該檔案的名稱包含建立檔案的時間戳。

將整個orders表匯出為將時間戳作為檔名的一部分的csv檔案。

set @ts = date_format(now(),'_%y%m%d_%h%i%s');

set @folder= 'f:/worksp/mysql/';

set @prefix= 'orders';

set @ext= '.csv';

set @cmd= concat("select * from orders into outfile '",@folder,@prefix,@ts,@ext,"' fields enclosed by '\"' terminated by ';' escaped by '\"'","lines terminated by '\r\n';");

prepare statement from @cmd;

execute statement;

首先,構造了乙個具有當前時間戳的查詢作為檔名的一部分。其次,使用prepare語句from命令準備執行語句。

最後,使用execute命令執行語句。

使用列標題匯出資料

要新增列標題,需要使用union語句如下:

(select 'order number','order date','status')

union

(select ordernumber,orderdate, status

from orders

into outfile'f:/worksp/mysql/orders_union_title.csv'fields enclosed by'"' terminated by ';' escaped by '"'lines terminated by'\r\n');

處理null值

如果結果集中的值包含null值,則目標檔案將使用「n/a」來代替資料中的null值。要解決此問題,您需要將null

select

ordernumber, orderdate, ifnull(shippeddate,'n/a')

from

orders into outfile'f:/worksp/mysql/orders_null2na.csv'fields enclosed by'"'terminated by';'escaped by'"'lines

terminated by'\r\n';

用n/a字串替換了shippingdate列中的null值。 csv檔案將顯示n/a而不是null值。

值替換為另乙個值,例如不適用(n/a),方法是使用ifnull函式,如下:

mysql備份檔案夾 mysql 備份

mysql定期備份是一項重要的工作,但人工操作太繁瑣,也難避免有所疏漏,使用下面的方法即可讓系統定期備份資料。1 建立備份檔案夾 cd www makedir backup 2 編寫執行指令碼 vi autobackup 寫入以下內容 filename date y m d mysql bin di...

mysql的bht資料夾 MySQL基本操作

1.有密碼的登入 mysql u 使用者名稱 p 提示輸入密碼 登入成功後顯示 welcome to the mysql monitor.commands end with or g.your mysql connection id is xx server version 5.6.13 mysql...

利用遞迴刪除資料夾(資料夾中套資料夾)

刪除目錄 bool deldir const ansistring p if p.isempty p.length 4 return false 引數必須大於3,即不能為磁碟根目錄或空白 int len p.length char path p.c str ansistring dir ansist...