Phpmysql data import and export, data table structure import and export. To import and export data, *********************************** * ******************* contains the Mysql database operation file require_once (Mysql implements data import and export, data table structure import and export
**************************************** ****************/
//
// Contains the Mysql database operation file
//
Require_once ("MysqlDB. php ");
/*************************************** ****************
** Class name: MysqlDB
** Category code: lcq-DB-003
** Usage: database connection establishment, data operations, number of retrieved fields, and number of records
** Author: Lin Chao-Qi
** Prepared on)
**************************************** ****************/
Class DBManagement implements IDBManagement
{
//
// Name of all data tables in the current database
//
Private $ TablesName;
//
// Default path
//
Private $ defapath path;
//
// Name of the database to be operated
//
Private $ DatabaseName;
//
// Database operation object
//
Private $ db;
/*************************************** ****************
** Method name: __construct
** Function description: Create a DBManagement object.
** Input parameter: $ _ DatabaseName-string <要操作的数据库名,如果为空则从配置文件中读取>
** $ _ DefaultPath-string <存储数据的默认路径,如果为空则从配置文件中读取>
** Output parameters: None
** Return value: None
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
Function _ construct ($ _ DatabaseName = "", $ _ defapath path = "")//
{
Require ("config. inc. php ");
If (! $ _ DatabaseName) {$ this-> DatabaseName = $ dbName ;}
Else {$ this-> DatabaseName =$ _ DatabaseName ;}
If (! $ _ DefaultPath) {$ this-> DefaultPath = $ defaultPath ;}
Else {$ this-> DefaultPath =$ _ defapath path ;}
$ Path = realpath ($ this-> DefaultPath );
$ This-> DefaultPath = str_replace ("", "/", $ path );
// $ This-> db = new DBFactory ();
$ This-> db = new MysqlDB ();
}
/*************************************** ****************
** Method name: GetTablesName
** Function description: Get the names of all data tables in $ this-> Database.
** Input parameter: None
** Output parameters: None
** Return value:-array <$ this-> TablesName: $ this-> name of all data tables in the Database>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
Protected function GetTablesName ()
{
$ Result = $ this-> db-> Query ("show table status ");
While ($ Row = $ this-> db-> NextRecord ($ result ))
{
$ This-> TablesName [] = $ Row ["Name"];
}
Return $ this-> TablesName;
}
/*************************************** ****************
** Method: GetDataFileName
** Function description: obtain the physical file name of the data file corresponding to all data tables of $ this-> Database.
** Input parameter: None
** Output parameters: None
** Return value:-array <$ DataFilesName: $ physical file name corresponding to all data tables in the Database>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
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;
}
/*************************************** ****************
** Method: SaveTableStructure
** Function description: save the data table structure to the install. SQL file. the target path is $ DefaultPath.
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示保存成功;
** If it is set to false, the storage fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
Protected function SaveTableStructure ($ text)
{
$ FileName = $ this-> DefaultPath. "/Install. SQL ";
// If (file_exists ($ fileName ))
//{
// Unlink ($ fileName );
//}
// Echo $ text;
$ Fp = fopen ($ fileName, "w + ");
Fwrite ($ fp, $ text );
}
/*************************************** ****************
** Method: RestoreTableStructure
** Function description: backup the structure of all data tables in the Database $ this->
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示所有数据表的结构备份成功;
** False indicates that the structure backup of all or some data tables fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
Protected function BackupTableStructure ()
{
$ I = 0;
$ SqlText = "";
$ This-> GetTablesName ();
$ Count = count ($ this-> TablesName );
//
// Retrieve the structure of all data tables
//
While ($ I <$ count)
{
$ TableName = $ this-> TablesName [$ I];
$ Result = $ this-> db-> Query ("show create table $ tableName ");
$ This-> db-> NextRecord ($ result );
//
// Obtain the SQL statement of the generated table
//
$ 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 );
}
/*************************************** ****************
** Method: RestoreTableStructure
** Function description: restore the structure of all data tables in $ this-> Database.
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示所有数据表的结构还原成功;
** If it is set to false, structure restoration of all or some data tables fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
Protected function RestoreTableStructure ()
{
$ FileName = $ this-> DefaultPath. "/Install. SQL ";
If (! File_exists ($ fileName) {echo "the table structure file cannot be found. please make sure you have backed up before! "; 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 );
//
// The Last of the array is ";", which is a useless statement,
//
For ($ I = 1; $ I <$ count; $ I ++)
{
$ SQL = $ sqlArray [$ i-1]. ";";
$ Result = $ this-> db-> ExecuteSQL ($ SQL );
If (! Mysql_errno ())
{
If ($ I % 2 = 0) {echo "Data Table". ($ I/2). "structure restored successfully! N ";}
}
Else
{
If ($ I % 2 = 0)
{
Echo "Data Table". ($ I/2). "structure recovery failed! N ";
Exit ();
}
}
}
}
Catch (Exception $ e)
{
$ This-> db-> ShowError ($ e-> getMessage ());
$ This-> db-> Close ();
Return false;
}
}
/*************************************** ****************
** Method name: ImportData
** Function description: import all data in the database table from the. txt file with the same name. the Source path is $ DefaultPath.
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示所有数据表的数据导入成功;
** If it is set to false, data import to all or some data tables fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
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 "data imported successfully! ";
}
Catch (Exception $ e)
{
$ This-> db-> ShowError ($ e-> getMessage ());
Return false;
Exit ();
}
}
}
/*************************************** ****************
** Method: ExportData
** Function description: export the data of all data tables in $ this-> databaseto the. txt file with the same name. The target path is $ DefaultPath.
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示所有数据表的数据导出成功;
** If it is set to false, data export of all or some data tables fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-09
** Modifier:
** Date:
**************************************** ****************/
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 "data exported successfully! ";
}
Catch (Exception $ e)
{
$ This-> db-> ShowError ($ e-> getMessage ());
Return false;
Exit ();
}
}
/*************************************** ****************
** Method name: BackupDatabase
** Function description: backup the structure and data of all data tables in the Database.
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示所有数据表的数据库备份成功;
** If it is set to false, database backup for all or some data tables fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-10
** Modifier:
** Date:
**************************************** ****************/
Function BackupDatabase ()
{
Try
{
$ This-> BackupTableStructure ();
$ This-> ExportData ();
Return true;
}
Catch (Exception $ e)
{
$ This-> db-> ShowError ($ e-> getMessage ());
Return false;
Exit ();
}
}
/*************************************** ****************
** Method name: RestoreDatabase
** Function description: restore the structure and data of all data tables in the Database.
** Input parameter: None
** Output parameters: None
** Return value:-bool <返回为true表示所有数据表的数据库还原成功;
** False indicates that the restoration of all or some data tables fails.>
** Author: Lin Chao-Qi
** Date: 2007-04-10
** Modifier:
** Date:
**************************************** ****************/
Function RestoreDatabase ()
{
Try
{
$ This-> RestoreTableStructure ();
$ This-> ImportData ();
Return true;
}
Catch (Exception $ e)
{
$ This-> db-> ShowError ($ e-> getMessage ());
Return false;
Exit ();
}
}
}
?> [/Php]
Export and import of Shard data, *********************************** * ******************* // contains the Mysql database operation file // require_once (Mysql...