(原创)php数据库(mysql)分卷导入类及思路详解

日期 2012年05月17日 02:17

分类 PHP

标签 原创

浏览 6449

字数统计: 6240(字)

本文发布于 12 年前, 内容可能已经过时或失效!

数据库导入导出是一个后台必要拥有的功能,网上一搜,有很多关于数据库导入导出的,但基本上一个大的系统,包含了许多我们并不需要的,而且他们都是自己的后台的形式,我并不喜欢的是拿人家的东西整合到自己的后台,我需要的是自己东西。于是参照了很多,自己写了一个关于分卷导入类。以方便调用。欢迎大家拍砖。

具体完整实现mysql导入导出功能见: http://yanue.net/post-19.html

// 分别是主机,用户名,密码,数据库名,数据库编码
$db = new DataManage ( 'localhost', 'root', 'root', 'test', 'utf8' );
// sql文件,是否只导入单个sql(即如果有其他分卷也不导入)
$db->restore ( './backup/20120516211738_all_v1.sql', false );

这里针对分卷文件是以‘_v1.sql’为结尾,实现单个sql文件及分卷sql导入,分卷导入可选择是否当前分卷导入余下分卷,我们只需要直接调用类即可完成

对应如何去列出备份的sql文件或选择sql之类的,自己去实现,那个不在这个范畴了,也很简单的。

实际演示效果:
image
image

还有目前只实现了数据库导入,关于数据库导出的,正在编写功能。

下面是完整的类代码:(具体思路及实现代码里面都有说明,这里不在赘述)

<?php
/**
 * @author yanue
 * @copyright   Copyright (c) 2012 www.yanue.net
 * @link  http://www.yanue.net/archives/150.html
 * 说明:分卷文件是以_v1.sql为结尾
 * 功能:实现单个sql文件及分卷sql导入,分卷导入可选择是否当前分卷导入余下分卷
 * 使用方法:
 *
 *
 * ------------------------------------------------------------------
//分别是主机,用户名,密码,数据库名,数据库编码
$db = new DataManage ( 'localhost', 'root', 'root', 'test', 'utf8' );
//sql文件,是否只导入单个sql(即如果有其他分卷也不导入)
$db->restore ( './backup/20120516211738_all_v1.sql', false );
 *----------------------------------------------------------------------
 */
class DataManage {
        var $db; // 数据库连接
        var $database; // 所用数据库
        var $sqldir; // 数据库备份文件夹

        /**
         * 初始化
         *
         * @param string $host
         * @param string $username
         * @param string $password
         * @param string $database
         * @param string $charset
         */
        function __construct($host = 'localhost', $username = 'root', $password = '', $database = 'test', $charset = 'utf8') {
                $this->host = $host;
                $this->username = $username;
                $this->password = $password;
                $this->database = $database;
                $this->charset = $charset;
                // 连接数据库
                $this->db = mysql_connect ( $this->host, $this->username, $this->password ) or die ( "数据库连接失败." );
                // 选择使用哪个数据库
                mysql_select_db ( $this->database, $this->db ) or die ( "无法打开数据库" );
                // 数据库编码方式
                mysql_query ( 'SET NAMES ' . $this->charset, $this->db );
        }

        /**
         * 导入备份数据
         * 说明:分卷文件格式20120516211738_all_v1.sql
         *
         * @param string $sqlfile
         * @param bool $single
         */
        function restore($sqlfile, $single = FALSE) {
                // 检测文件是否存在
                if (! file_exists ( $sqlfile )) {
                        exit ( "文件不存在!请检查" );
                }
                $this->lock ( $this->database );
                // 获取数据库存储位置
                $sqlpath = pathinfo ( $sqlfile );
                $this->sqldir = $sqlpath ['dirname'];
                // 检测是否包含分卷,将类似20120516211738_all_v1.sql从_v分开,有则说明有分卷
                $volume = explode ( "_v", $sqlfile );
                $volume_path = $volume [0];
                echo "请勿刷新及关闭浏览器以防止程序被中止,如有不慎!将导致数据库结构受损<br />";
                echo "正在导入备份数据,请稍等!<br />";
                if (empty ( $volume [1] ) || $single) {
                        echo "正在导入sql:<span style='color:#f00;'>" . $sqlfile . '</span><br />';
                        // 没有分卷
                        if ($this->_import ( $sqlfile )) {
                                echo "数据库导入成功!";
                        } else {
                                exit ( '数据库导入失败!' );
                        }
                } else {
                        // 存在分卷,则获取当前是第几分卷,循环执行余下分卷
                        $volume_id = explode ( ".sq", $volume [1] );
                        // 当前分卷为$volume_id
                        $volume_id = intval ( $volume_id [0] );
                        while ( $volume_id ) {
                                $tmpfile = $volume_path . "_v" . $volume_id . ".sql";
                                // 存在其他分卷,继续执行
                                if (file_exists ( $tmpfile )) {
                                        // 执行导入方法
                                        echo "正在导入分卷$volume_id:<span style='color:#f00;'>" . $tmpfile . '</span><br />';
                                        if ($this->_import ( $tmpfile )) {

                                        } else {
                                                exit ( "导入分卷$volume_id:<span style='color:#f00;'>" . $tmpfile . '</span>失败!可能是数据库结构已损坏!请尝试从分卷1开始导入' );
                                        }
                                } else {
                                        echo "此分卷备份全部导入成功!<br />";
                                        return;
                                }
                                $volume_id ++;
                        }
                }
        }

        /**
         * 将sql导入到数据库(普通导入)
         *
         * @param string $sqlfile
         * @return boolean
         */
        private function _import($sqlfile) {
                $name = basename ( $sqlfile );
                $sqls = file ( $sqlfile );
                foreach ( $sqls as $sql ) {
                        str_replace ( "\r", "", $sql );
                        str_replace ( "\n", "", $sql );
                        if (! mysql_query ( trim ( $sql ), $this->db ))
                                return false;
                }
                return true;
        }

        // 关闭数据库连接
        private function close() {
                mysql_close ( $this->db );
        }

        // 锁定数据库,以免备份或导入时出错
        private function lock($tablename, $op = "WRITE") {
                if (mysql_query ( "lock tables " . $tablename . " " . $op ))
                        return true;
                else
                        return false;
        }

        // 解锁
        private function unlock() {
                if (mysql_query ( "unlock tables" ))
                        return true;
                else
                        return false;
        }

        // 析构
        function __destruct() {
                mysql_query ( "unlock tables", $this->db );
                mysql_close ( $this->db );
        }
}