實現
資料的匯入匯出,資料表結構的匯入匯出
********************************************************/
//
//包含Mysql
資料庫操作檔案
//
require_once("MysqlDB.php");
/*******************************************************
**類 名:MysqlDB
**類 編 號:lcq-DB-003
**作 用:資料庫
連結的建立,資料操作,擷取
欄位個數,記錄條數等
**作 者:林超旗
**編寫日期:(2007-04-07)
********************************************************/
class DBManagement implements IDBManagement
{
//
//當前資料庫中所有的資料表的名字
//
private $TablesName;
//
//預設路徑
//
private $DefaultPath;
//
//當前要操作的資料庫名
//
private $DatabaseName;
//
//操作資料庫的對象
//
private $db;
/*******************************************************
**方 法 名:__construct
**
功能描述:建立一個DBManagement的對象
**輸入參數:$_DatabaseName-string<要操作的資料庫名,如果為空白則從設定檔中讀取>
** $_DefaultPath-string<儲存資料的預設路徑,如果為空白則從設定檔中讀取>
**輸出參數:無
**返 回 值:無
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
function __construct($_DatabaseName="",$_DefaultPath="")//
{
require("config.inc.php");
if(!$_DatabaseName) {$this->DatabaseName=$dbName;}
else {$this->DatabaseName=$_DatabaseName;}
if(!$_DefaultPath) {$this->DefaultPath=$defaultPath;}
else {$this->DefaultPath=$_DefaultPath;}
$path=realpath($this->DefaultPath);
$this->DefaultPath=str_replace("","/",$path);
//$this->db=new DBFactory();
$this->db=new MysqlDB();
}
/*******************************************************
**方 法 名:GetTablesName
**功能描述:擷取$this->Database的所有資料表的名字
**輸入參數:無
**輸出參數:無
**返 回 值:-array <$this->TablesName:$this->Database的所有資料表的名字>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
protected function GetTablesName()
{
$result=$this->db->Query("show table status");
while($Row=$this->db->NextRecord($result))
{
$this->TablesName[]=$Row["Name"];
}
return $this->TablesName;
}
/*******************************************************
**方 法 名:GetDataFileName
**功能描述:擷取與$this->Database的所有資料表對應的資料檔案的物理檔案名稱
**輸入參數:無
**輸出參數:無
**返 回 值:-array <$DataFilesName:$與$this->Database的所有資料表對應的資料檔案的物理檔案名稱>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
protected function GetDataFileName()
{
$this->GetTablesName();
$count=count($this->GetTablesName());
for ($i=0;$i<$count;$i++)
{
$DataFilesName[]=$this->DefaultPath."/".$this->TablesName[$i].".txt";
//echo $DataFilesName[$i];
}
return $DataFilesName;
}
/*******************************************************
**方 法 名:SaveTableStructure
**功能描述:儲存資料表的結構到install.sql檔案中,目標路徑為$DefaultPath
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示儲存成功;
** 為false表示儲存失敗>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
protected function SaveTableStructure($text)
{
$fileName=$this->DefaultPath."/Install.sql";
//if(file_exists($fileName))
//{
// unlink($fileName);
//}
//echo $text;
$fp=fopen($fileName,"w+");
fwrite($fp,$text);
}
/*******************************************************
**方 法 名:RestoreTableStructure
**功能描述:備份$this->Database中所有資料表的結構
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示所有資料表的結構備份成功;
** 為false表示全部或部分資料表的結構備份失敗>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
protected function BackupTableStructure()
{
$i=0;
$sqlText="";
$this->GetTablesName();
$count=count($this->TablesName);
//
//取出所有資料表的結構
//
while($i<$count)
{
$tableName=$this->TablesName[$i];
$result=$this->db->Query("show create table $tableName");
$this->db->NextRecord($result);
//
//取出成生表的
SQL語句
//
$sqlText.="DROP TABLE IF EXISTS `".$tableName."`; ";//`
$sqlText.=$this->db->GetField(1)."; ";
$i++;
}
$sqlText=str_replace("r","",$sqlText);
$sqlText=str_replace("n","",$sqlText);
//$sqlText=str_replace("'","`",$sqlText);
$this->SaveTableStructure($sqlText);
}
/*******************************************************
**方 法 名:RestoreTableStructure
**功能描述:還原$this->Database中所有資料表的結構
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示所有資料表的結構還原成功;
** 為false表示全部或部分資料表的結構還原失敗>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
protected function RestoreTableStructure()
{
$fileName=$this->DefaultPath."/Install.sql";
if(!file_exists($fileName)){echo "找不到表結構檔案,請確認你以前做過備份!";exit;}
$fp=fopen($fileName,"r");
$sqlText=fread($fp,filesize($fileName));
//$sqlText=str_replace("r","",$sqlText);
//$sqlText=str_replace("n","",$sqlText);
$sqlArray=explode("; ",$sqlText);
try
{
$count=count($sqlArray);
//
//數組的最後一個為";",是一個無用的語句,
//
for($i=1;$i<$count;$i++)
{
$sql=$sqlArray[$i-1].";";
$result=$this->db->ExecuteSQL($sql);
if(!
mysql_errno())
{
if($i%2==0){echo "資料表".($i/2)."的結構恢複成功!n";}
}
else
{
if($i%2==0)
{
echo "資料表".($i/2)."的結構恢複失敗!n";
exit();
}
}
}
}
catch(Exception $e)
{
$this->db->ShowError($e->getMessage());
$this->db->Close();
return false;
}
}
/*******************************************************
**方 法 名:ImportData
**功能描述:匯入$this->Database中所有資料表的資料從與其同名的.txt檔案中,源路徑為$DefaultPath
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示所有資料表的資料匯入成功;
** 為false表示全部或部分資料表的資料匯入失敗>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
function ImportData()
{
$DataFilesName=$this->GetDataFileName();
$count=count($this->TablesName);
//$this->db->ExecuteSQL("set character set gbk");
for ($i=0;$i<$count;$i++)
{
//$DataFilesName[$i]=str_replace("","/",$DataFilesName[$i])
//echo $DataFilesName[$i];
$sqlLoadData="load data infile '".$DataFilesName[$i]."' into table ".$this->TablesName[$i];
$sqlCleanData="delete from ".$this->TablesName[$i];
//echo $sql."n";
try
{
//$this->db->ExecuteSQL("set character set utf8");
//$this->db->ExecuteSQL("SET NAMES 'utf8'");
$this->db->ExecuteSQL($sqlCleanData);
$this->db->ExecuteSQL($sqlLoadData);
return true;
//echo "資料匯入成功!";
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
}
/*******************************************************
**方 法 名:ExportData
**功能描述:匯出$this->Database中所有資料表的資料到與其同名的.txt檔案中,目標路徑為$DefaultPath
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示所有資料表的資料匯出成功;
** 為false表示全部或部分資料表的資料匯出失敗>
**作 者:林超旗
**日 期:2007-04-09
**修 改 人:
**日 期:
********************************************************/
function ExportData()
{
$DataFilesName=$this->GetDataFileName();
$count=count($this->TablesName);
try
{
for ($i=0;$i<$count;$i++)
{
$sql="select * from ".$this->TablesName[$i]." into outfile '".$DataFilesName[$i]."'";
if(file_exists($DataFilesName[$i]))
{
unlink($DataFilesName[$i]);
}
//$this->db->ExecuteSQL("SET NAMES 'utf8'");
$this->db->ExecuteSQL($sql);
}
return true;
//echo "資料匯出成功!";
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
/*******************************************************
**方 法 名:BackupDatabase
**功能描述:備份$this->Database中所有資料表的結構和資料
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示所有資料表的Database Backup成功;
** 為false表示全部或部分資料表的Database Backup失敗>
**作 者:林超旗
**日 期:2007-04-10
**修 改 人:
**日 期:
********************************************************/
function BackupDatabase()
{
try
{
$this->BackupTableStructure();
$this->ExportData();
return true;
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
/*******************************************************
**方 法 名:RestoreDatabase
**功能描述:還原$this->Database中所有資料表的結構和資料
**輸入參數:無
**輸出參數:無
**返 回 值:- bool<返回為true表示所有資料表的資料庫還原成功;
** 為false表示全部或部分資料表的資料庫還原失敗>
**作 者:林超旗
**日 期:2007-04-10
**修 改 人:
**日 期:
********************************************************/
function RestoreDatabase()
{
try
{
$this->RestoreTableStructure();
$this->ImportData();
return true;
}
catch (Exception $e)
{
$this->db->ShowError($e->getMessage());
return false;
exit();
}
}
}
?>[/php]
http://www.bkjia.com/PHPjc/444994.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444994.htmlTechArticle實現 資料 的匯入匯出,資料表結構的匯入匯出 ********************************************************/ // //包含Mysql 資料庫 操作檔案 // require_once(Mysql...