ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

php 数据批量插入mysql和mysql类

2021-01-21 10:34:40  阅读:221  来源: 互联网

标签:return 批量 param strSql mysql debug table array php


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

 

标签:return,批量,param,strSql,mysql,debug,table,array,php
来源: https://www.cnblogs.com/jackzhuo/p/14306671.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有