php 與 mysql 入門學習指南

15
PHP 與 MySql 與與與與與與 PHP PHP MySQL MySQL 與與與與與與 與與與與與與 與 28 與 與與與與與與與與 與與與與 與 與與與與 與

Upload: lila-oconnor

Post on 30-Dec-2015

39 views

Category:

Documents


3 download

DESCRIPTION

PHP 與 MySQL 入門學習指南. 第 28 章 資料的匯入與匯出. 凱文瑞克 著. 資料匯入方法. Bulk Copy Protocol(BCP) 。 透過轉換程式來轉換不同檔案格式的資料庫檔案 。 將資料匯出,使其成為其它資料庫可接受之特定格式的檔案,然後再到新的資料庫系統中將資料匯入。 透過 XML 敘述展現資料庫中的資料。. MySQL 資料匯入的方法 (1). 以批次模式匯入資料 (1) 插入資料的 SQL 敘述存成一個文字檔。 USE Meet_A_Geek; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

PHPPHP 與與 MySQLMySQL 入門學習指南入門學習指南

第 28 章 資料的匯入與匯出

凱文瑞克 著凱文瑞克 著

Page 2: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

資料匯入方法 Bulk Copy Protocol(BCP) 。 透過轉換程式來轉換不同檔案格式的資料庫檔案 。將資料匯出,使其成為其它資料庫可接受之特定格式的檔案,然後再到新的資料庫系統中將資料匯入。透過 XML 敘述展現資料庫中的資料。

Page 3: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (1) 以批次模式匯入資料

(1) 插入資料的 SQL 敘述存成一個文字檔。

USE Meet_A_Geek;INSERT INTO Customers (Customer_ID, Last_Name)VALUSE(NULL,"Block");INSERT INTO Customers (Customer_ID, Last_Name)VALUSE(NULL,"Newton");INSERT INTO Customers (Customer_ID, Last_Name)VALUSE(NULL,"Simmons");

Page 4: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (2)儲存至 New_Data.sql 這個檔案中,接著切換至 mysql 目錄中,然後輸入下面指令:

 bin/mysql –p < New_Data.sql

New_Data.sql 中一連串的 SQL 敘述會送給 MySQL 去執行,比使用 MySQL monitor 一筆筆輸入來的方便。

Page 5: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (3)利用 mysqlimport

mysqlimport (option1 option2…………) <資料庫的名稱 > < 文字檔的檔案名稱 >

這個工具是使用文字檔名作為資料表名稱,文字檔中的資料型別及欄位數目也要跟要預備匯入的資料表數量相符,否則會發生錯誤而造成無法匯入資料的情形。所以用 mysqlimport 匯入資料之前一定要確定文字檔內的資料格式是正確無誤的。

Page 6: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (4)選項 選項用途

-d 或 --delete 匯入文字檔內資料之前,先刪除要匯入之資料表後的資料。

-f   或 --force 不管發生甚麼錯誤, mysqlimport 仍繼續匯入文字檔內的資料。

-i   或 --igonre    如果匯入過程中發現資料已存在資料表中,則略過文字檔中該筆資料繼續下一筆資料匯入動作。

-L 或 -local     這個選項將強迫從本地端文字檔匯入資料至遠端 MySQL 資料庫中,這樣轉入資料速度是較慢的,但是卻可以省去須先文字檔上傳至 MySQL Server 所在伺服器中的時間。

-l 或 -lock-tables 告訴 MySQL 在轉入資料之前,先行鎖定要匯入之資料表,這樣可以防止於資料匯入當中,有入修改資料表中的資料而造成錯誤。

-r 或 -replace     這個選項跟 -I 選項的用途正好相反,它會以文字檔中該筆資料取代資料表中的資料。

--fields-enclosed-by= 字元 A 

設定字元 A 為要匯入文字檔中欄位資料的分隔字元。

--fields-escaped-by= 字元A  

設定字元 A 為文字檔中特殊符號的跳脫字元 (escape character) ,用來解除要匯入資料中特殊符號的原先所代表的 意義,例如「 \ 」表示反斜線,「 \n 」表示換行。預設是無任何跳脫字元。

--fields-optionally-by= 字元 A

設定字元 A 為文字檔中欄位資料的分隔字元。預設分隔字元是 TAB

--lines-terminated-by定義要匯入文字檔中,每筆紀錄的分隔字串,預設值是換行字元 (new line) ,這裡也可指定 new line+carriage return 作為分隔字串。

Page 7: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (5)'1','James','Ponds','2','Tom','Johns','3','Rick','Asley','4','Billy','Ocean',

\mysql\bin\mysqlimport -rl --fields-enclosed-by=' --fields-terminated-by=, test customers

Page 8: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (6)由資料嵌入檔匯入資料

完整語法如下︰LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.t

xt'[REPLACE | IGNORE]INTO TABLE tbl_name[FIELDS[TERMINATED BY '\t'][[OPTIONALLY] ENCLOSED BY ''][ESCAPED BY '\\' ]][LINES TERMINATED BY '\n'][IGNORE number LINES][(col_name,...)]

Page 9: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (7)1 James Ponds2 Tom Johns3 Rick Asley4 Billy Ocean切換工作資料庫至 TEST : USE TEST;再輸入下面指令: LOAD DATA INFILE " 完整路徑 /customers2.txt" INTO TABLE custome

rs;

這樣會將 customers2.txt 檔案中的資料新增插入 customers 資料表中 ,

Page 10: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (8)關鍵字 FIELDS 宣告重要的設定說明如下:

TERMINATED BY <char> 表示以字元為匯入資料檔每筆紀錄間的欄位分隔字元 , 預設值是 tab 字元 (\t) 。ENCLOSED BY <char> 則是在選取範圍內若有被框的字元時使用。

Page 11: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯入的方法 (9)範例: ( 注意 , 這裡使用 customers.txt, 而不是 customers2.txt)

LOAD DATA INFILE " 完整路徑 /customers.txt" INTO TABLE customers FIELDS TERMINATED BY ',' ENCLOSED BY '"';

可以設定一些參數來控制資料匯入的動作:LOAD DATA LOCAL INFILE " 完整路徑 /customers.t

xt" INTO TABLE REMOTE_TABLE;

Page 12: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯出方法 (1)MySQL 資料匯出工具 【 mysqldump 】

完整語法如下所示:

mysqldump [OPTIONS] database [tables] > textfilename

Page 13: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯出方法 (2)要將資料庫 test 整個傾印出來 , 只要輸入: bin/mysqldump test > 文字檔檔案名稱 -- MySQL dump 8.19

---- Host: localhost Database: test----------------------------------------------------------- Server version 4.0.1-alpha---- Table structure for table 'customers'--CREATE TABLE customers ( id int(11) default NULL, first_name varchar(20) default NULL, last_name varchar(20) default NULL) TYPE=MyISAM;

/*!40000 ALTER TABLE customers DISABLE KEYS */;---- Dumping data for table 'customers'--INSERT INTO customers VALUES (1,'James','Ponds\"\r\n\"2');INSERT INTO customers VALUES (1,'James','Ponds');INSERT INTO customers VALUES (2,'Tom','Johns');INSERT INTO customers VALUES (1,'James','Ponds');

Page 14: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯出方法 (3)例如︰可以利用 WHERE 敘述來選擇性的匯出資料:

bin/mysqldump -where=" 欄位名稱 > 2000" 資料庫名稱 資料表明稱 > 文字檔檔案名稱 或將匯出的資料以 , 號隔開 , 例如:

bin/mysqldump --tab=/temp --fields-terminated-by=, test customers

執行後在 temp 路徑下會產生 customers.txt 及 customers.sql , customers.txt 即是資料檔 , 格式如下所示:

1','James','Ponds2','Tom','Johns3','Rick','Asley4','Billy','Ocean

若加上 -t 選項 , 則不會產生 customers.sql , 命令如下所示: bin/mysqldump -t --tab=/temp --fields-terminated-by=, test customers

Page 15: PHP 與 MySQL 入門學習指南

PHP 與 MySql 入門學習指南

MySQL 資料匯出方法 (4)利用【 SELECT OUTFILE 】匯出資料

SELECT * INTO OUTFILE " 完整路徑 /result.txt"FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'LINES TERMINATED BY "\n"FROM customers;

執行後 result.txt 的內容類似如下格式:1,"James","Ponds"2,"Tom","Johns"3,"Rick","Asley"4,"Billy","Ocean"