1. 程式人生 > 資料庫 >php 資料批量插入mysql和mysql類

php 資料批量插入mysql和mysql類

1.資料批量插入mysql

第一步:配置my.ini檔案

檔案中配置

bulk_insert_buffer_size=120M 或者更大

將insert語句的長度設為最大。

Max_allowed_packet=1M

Net_buffer_length=8k

第二步,批量插入資料時使用多條模式:

INSERT INTO table (field1,field2,field3) VALUES ('a',"b","c"), ('a',"b","c"),('a',"b","c");

拼接方式如下:

    /**
     * 多條資料同時轉化成插入SQL語句
     * @param string $table 表名
     * @$arr_key是表字段名的key:$arr_key=array("field1","field2","field3")
     * @param array $arr是要插入的欄位值 陣列示例 array(("a","b","c"), ("bbc","bbb","caaa"),('add',"bppp","cggg"))
     * @return string
     */
    function multArray2Insert($table,$arr_key, $arr, $split = '`',$debug = false) {
        $arrValues = array();
        if (empty($table) || !is_array($arr_key) || !is_array($arr)) {
            return false;
        }
        $sql = "INSERT INTO %s( %s ) values %s ";
        foreach ($arr as $k => $v) {
            $arrValues[$k] = "'".implode("','",array_values($v))."'";

        }
        $sql = sprintf($sql, $table, "{$split}" . implode("{$split} ,{$split}", $arr_key) . "{$split}", "(" . implode(") , (", array_values($arrValues)) . ")");
        //var_dump($sql);exit;
        if ($debug === true) $this->debug($sql);
        $result = $this->dbh->exec($sql);
        $this->getPDOError();
        return $result;
    }

$arr可以這麼拼接:

$dataArr[]=[
    $short_id,
    $nickname,
    $text,
    1,
    1,
    $createTme,
    $awemeId,
    $getTime,
    ;

下面是本人常用的mysql類庫,有時候寫原生的php時使用。基於PDO。

<?php
class PDOmysql
{
    private $host = "";//連線地址
    private $user = "";//使用者名稱
    private $pass = "";//密碼
    public $dbcharset = "utf8";//預設的資料庫編碼

    protected static $_instance = null;//資料庫例項
    protected $dsn;
    public $dbh = null;

    /**
     * 構造
     * @param $dbname 資料庫名
     * @param array $dbconf 資料庫連線資訊
     * @throws Exception
     */
    private function __construct($dbname, $dbconf = array())
    {
        $host = isset($dbconf['host']) ? $dbconf['host'] : $this->host;
        $user = isset($dbconf['user']) ? $dbconf['user'] : $this->user;
        $pass = isset($dbconf['pass']) ? $dbconf['pass'] : $this->pass;
        try {
            $this->dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;
            $this->dbh = new PDO($this->dsn, $user, $pass);
            $this->dbh->exec('SET character_set_connection=' . $this->dbcharset . ', character_set_results=' . $this->dbcharset . ', character_set_client=binary');
        } catch (PDOException $e) {
            $this->outputError($e->getMessage());
        }
    }

    /**
     * Singleton instance
     *
     * @param $dbname
     * @param array $dbconf 資料庫連線資訊
     * @return null|PDOmysql 存放資料庫例項
     * @throws Exception
     */
    public static function getInstance($dbname, $dbconf = array())
    {
        if (!self::$_instance) {
            self::$_instance = new self($dbname, $dbconf);
        }
        return self::$_instance;
    }

    /**
     * Query 查詢
     *
     * @param String $strSql SQL語句
     * @param String $queryMode 查詢方式(All or Row)
     * @param int $pdoMode 指定資料獲取方式
     * @param Boolean $debug
     * @return Array
     */
    public function query($strSql, $queryMode = 'All', $pdoMode = PDO::FETCH_ASSOC, $debug = false)
    {
        if ($debug === true) $this->debug($strSql);
        $recordset = $this->dbh->query($strSql);
        $this->getPDOError();
        if ($recordset) {
            $recordset->setFetchMode($pdoMode);
            if ($queryMode == 'All') {
                $result = $recordset->fetchAll();
            } elseif ($queryMode == 'Row') {
                $result = $recordset->fetch();
            }
        } else {
            $result = null;
        }
        return $result;
    }

    /**
     * Update 更新
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 欄位與值
     * @param String $where 條件
     * @param Boolean $debug
     * @return Int
     * @throws Exception
     */
    public function update($table, $arrayDataValue, $where = '', $debug = false)
    {
//        print_r($arrayDataValue);
        $this->checkFields($table, $arrayDataValue);
        if ($where) {
            $strSql = '';
            foreach ($arrayDataValue as $key => $value) {
                if (!empty($value)) {
                    $strSql .= ", $key='$value'";
                } else {
                    $strSql .= ", $key=null";
                }
            }
            $strSql = substr($strSql, 1);
            $strSql = "UPDATE $table SET $strSql WHERE $where";
//            echo $strSql;
        } else {
            $strSql = "REPLACE INTO $table ('" . implode(',', array_keys($arrayDataValue)) . "') VALUES ('" . implode(',', $arrayDataValue) . "')";
        }
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }

    /**
     * Insert 插入
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 欄位與值
     * @param Boolean $debug
     * @return Int
     * @throws Exception
     */
    public function insert($table, $arrayDataValue, $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        $strSql = "INSERT INTO `$table` (`" . implode('`,`', array_keys($arrayDataValue)) . "`) VALUES ('" . implode("','", $arrayDataValue) . "')";
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }


    /**
     * 批量插入
     * 多條資料同時轉化成插入SQL語句
     * @param string $table 表名
     * @$arr_key是表字段名的key:$arr_key=array("field1","field2","field3")
     * @param array $arr是欄位值 陣列示例 array(("a","b","c"), ("bbc","bbb","caaa"),('add',"bppp","cggg"))
     * @return string
     */
    function multArray2Insert($table,$arr_key, $arr, $split = '`',$debug = false) {
        $arrValues = array();
        if (empty($table) || !is_array($arr_key) || !is_array($arr)) {
            return false;
        }
        $sql = "INSERT INTO %s( %s ) values %s ";
        foreach ($arr as $k => $v) {
            $arrValues[$k] = "'".implode("','",array_values($v))."'";

        }
        $sql = sprintf($sql, $table, "{$split}" . implode("{$split} ,{$split}", $arr_key) . "{$split}", "(" . implode(") , (", array_values($arrValues)) . ")");
        //var_dump($sql);exit;
        if ($debug === true) $this->debug($sql);
        $result = $this->dbh->exec($sql);
        $this->getPDOError();
        return $result;
    }


    /**
     * Replace 覆蓋方式插入
     *
     * @param String $table 表名
     * @param Array $arrayDataValue 欄位與值
     * @param Boolean $debug
     * @return Int
     * @throws Exception
     */
    public function replace($table, $arrayDataValue, $debug = false)
    {
        $this->checkFields($table, $arrayDataValue);
        $strSql = "REPLACE INTO `$table` (`" . implode('`,`', array_keys($arrayDataValue)) . "`) VALUES ('" . implode("','", $arrayDataValue) . "')";
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }


    /**
     * Delete 刪除
     *
     * @param String $table 表名
     * @param String $where 條件
     * @param Boolean $debug
     * @return Int
     * @throws Exception
     */
    public function delete($table, $where = '', $debug = false)
    {
        if ($where == '') {
            $this->outputError("'WHERE' is Null");
        } else {
            $strSql = "DELETE FROM `$table` WHERE $where";
            if ($debug === true) $this->debug($strSql);
            $result = $this->dbh->exec($strSql);
            $this->getPDOError();
            return $result;
        }
    }

    /**
     * execSql 執行SQL語句
     *
     * @param String $strSql
     * @param Boolean $debug
     * @return Int
     */
    public function execSql($strSql, $debug = false)
    {
        if ($debug === true) $this->debug($strSql);
        $result = $this->dbh->exec($strSql);
        $this->getPDOError();
        return $result;
    }

    /**
     * checkFields 檢查指定欄位是否在指定資料表中存在
     *
     * @param String $table
     * @param $arrayFields
     * @throws Exception
     */
    private function checkFields($table, $arrayFields)
    {
        $fields = $this->getFields($table);
        foreach ($arrayFields as $key => $value) {
            if (!in_array($key, $fields)) {
                $this->outputError("Unknown column `$key` in field list.");
            }
        }
    }

    /**
     * getFields 獲取指定資料表中的全部欄位名
     *
     * @param String $table 表名
     * @return array
     */
    private function getFields($table)
    {
        $fields = array();
        $recordset = $this->dbh->query("SHOW COLUMNS FROM $table");
        $this->getPDOError();
        $recordset->setFetchMode(PDO::FETCH_ASSOC);
        $result = $recordset->fetchAll();
        foreach ($result as $rows) {
            $fields[] = $rows['Field'];
        }
        return $fields;
    }

    /**
     * 獲取欄位最大值
     *
     * @param string $table 表名
     * @param string $field_name 欄位名
     * @param string $where 條件
     * @param bool $debug
     * @return int|mixed
     */
    public function getMaxValue($table, $field_name, $where = '', $debug = false)
    {
        $strSql = "SELECT MAX(" . $field_name . ") AS MAX_VALUE FROM $table";
        if ($where != '') $strSql .= " WHERE $where";
        if ($debug === true) $this->debug($strSql);
        $arrTemp = $this->query($strSql, 'Row');
        $maxValue = $arrTemp["MAX_VALUE"];
        if ($maxValue == "" || $maxValue == null) {
            $maxValue = 0;
        }
        return $maxValue;
    }

    /**
     * 獲取指定列的數量
     *
     * @param string $table
     * @param string $where
     * @param bool $debug
     * @return int
     */
    public function totalCount($table, $where = '', $debug = false)
    {
        $strSql = "SELECT COUNT(1) AS c FROM $table";
        if ($where != '') $strSql .= " WHERE $where";
        if ($debug === true) $this->debug($strSql);
        $arrTemp = $this->query($strSql, 'Row');
        return $arrTemp['c'];
    }

    /**
     * 獲取表引擎
     *
     * @param String $dbName 庫名
     * @param String $tableName 表名
     * @return String
     */
    public function getTableEngine($dbName, $tableName)
    {
        $strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name='" . $tableName . "'";
        $arrayTableInfo = $this->query($strSql);
        $this->getPDOError();
        return $arrayTableInfo[0]['Engine'];
    }

    /**
     * beginTransaction 事務開始
     */
    public function beginTransaction()
    {
        $this->dbh->beginTransaction();
    }

    /**
     * commit 事務提交
     */
    public function commit()
    {
        $this->dbh->commit();
    }

    /**
     * rollback 事務回滾
     */
    public function rollback()
    {
        $this->dbh->rollback();
    }

    /**
     * transaction 通過事務處理多條SQL語句
     * 呼叫前需通過getTableEngine判斷表引擎是否支援事務
     *
     * @param array $arraySql
     * @return Boolean
     */
    public function execTransaction($arraySql)
    {
        $retval = 1;
        $this->beginTransaction();
        foreach ($arraySql as $strSql) {
            if ($this->execSql($strSql) == 0) $retval = 0;
        }
        if ($retval == 0) {
            $this->rollback();
            return false;
        } else {
            $this->commit();
            return true;
        }
    }

    /**
     * getPDOError 捕獲PDO錯誤資訊
     */
    private function getPDOError()
    {
        if ($this->dbh->errorCode() != '00000') {
            $arrayError = $this->dbh->errorInfo();
            $this->outputError($arrayError[2]);
        }
    }

    /**
     * debug
     *
     * @param mixed $debuginfo
     */
    private function debug($debuginfo)
    {
        var_dump($debuginfo);
        exit();
    }

    /**
     * 輸出錯誤資訊
     *
     * @param String $strErrMsg
     * @throws Exception
     */
    private function outputError($strErrMsg)
    {
        throw new Exception('MySQL Error: ' . $strErrMsg);
    }

    /**
     * 防止克隆
     *
     */
    private function __clone()
    {
    }

    /**
     * destruct 關閉資料庫連線
     */
    public function __destruct()
    {
        $this->dbh = null;
    }

}
View Code