1 / 15

PHP 與 MySQL 入門學習指南

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

Download Presentation

PHP 與 MySQL 入門學習指南

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP與MySQL入門學習指南 第 28 章 資料的匯入與匯出 凱文瑞克 著

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

  3. 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");

  4. MySQL資料匯入的方法(2) • 儲存至New_Data.sql這個檔案中,接著切換至mysql目錄中,然後輸入下面指令: bin/mysql –p < New_Data.sql New_Data.sql 中一連串的SQL敘述會送給MySQL去執行,比使用 MySQL monitor 一筆筆輸入來的方便。

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

  6. MySQL資料匯入的方法(4)

  7. 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

  8. MySQL資料匯入的方法(6) • 由資料嵌入檔匯入資料 完整語法如下︰ LOAD DATA [LOW_PRIORITY | CONCURRENT] [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,...)]

  9. MySQL資料匯入的方法(7) 1 James Ponds 2 Tom Johns 3 Rick Asley 4 Billy Ocean 切換工作資料庫至 TEST: USE TEST; 再輸入下面指令: LOAD DATA INFILE "完整路徑/customers2.txt" INTO TABLE customers; 這樣會將 customers2.txt 檔案中的資料新增插入 customers 資料表中,

  10. MySQL資料匯入的方法(8) • 關鍵字 FIELDS 宣告重要的設定說明如下: • TERMINATED BY <char> 表示以字元為匯入資料檔每筆紀錄間的欄位分隔字元, 預設值是 tab 字元 (\t)。 • ENCLOSED BY <char> 則是在選取範圍內若有被框的字元時使用。

  11. MySQL資料匯入的方法(9) • 範例:(注意, 這裡使用 customers.txt, 而不是 customers2.txt) LOAD DATA INFILE "完整路徑/customers.txt" INTO TABLE customers FIELDS TERMINATED BY ',' ENCLOSED BY '"'; • 可以設定一些參數來控制資料匯入的動作: LOAD DATA LOCAL INFILE "完整路徑/customers.txt" INTO TABLE REMOTE_TABLE;

  12. MySQL 資料匯出方法(1) MySQL 資料匯出工具 【mysqldump】 • 完整語法如下所示: mysqldump [OPTIONS] database [tables] > textfilename

  13. 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');

  14. 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','Ponds 2','Tom','Johns 3','Rick','Asley 4','Billy','Ocean • 若加上 -t 選項, 則不會產生 customers.sql , 命令如下所示: bin/mysqldump -t --tab=/temp --fields-terminated-by=, test customers

  15. 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"

More Related