php 读取文件,批量构造sql语句写入数据库

需求读取文件,构造sql,批量写入数据库,批量是构建设置 $num 值的大小进行批量构建sql语句

test.txt 文件内容为

1
2
3
4
5
6
7
8
9
10
<?php
class batchInsertController {
    private $num = 3;
    private $table = 'test.t';
    public function batchInsert()
    {
        // read file
        ini_set('memory_limit', -1);
        $fh = fopen("./test.txt", "r");
        $i = 0;
        while (!feof($fh)) {
            $str = fgets($fh);
            if (!empty($str)) {
                $arr[] = $str;
                $i++;
                if ($i == $this->num) {
                    echo $i . PHP_EOL;
                    // 批量构造sql
                    $sql = $this->buildSql($arr);
                    print_r($arr);
                    //unset($arr);  有bug的地方就是在这里
                    if (!empty($sql)) {
                        // 提交sql
                        echo $sql;
                    }
                    $i = 0;
                }
            }
        }
    }
    protected function buildSql($arr)
    {
        if (!empty($arr)) {
            $sql = "INSERT INTO {$this->table} VALUES ";
            foreach ($arr as $key => $value) {
                $value = trim($value);
                $sql .= "('column1','$value','column3')";
                if ($key != count($arr) - 1) {
                    $sql .= ',';
                }
            }
        }
        return $sql;
    }
}
$a = new batchInsertController();
$a->batchInsert();

目标实现:
读取文件三行内容构建一条sql语句,然后写入数据库,那么数组的大小应该也应该是3个为一组
执行的结果为:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

数组为什么是这么大呢?不应该是3个一组么,应该每次进来初始化的

<?php
class batchInsertController {
    private $num = 3;
    private $table = 'test.t';
    public function batchInsert()
    {
        // read file
        ini_set('memory_limit', -1);
        $fh = fopen("./test.txt", "r");
        $i = 0;
        while (!feof($fh)) {
            $str = fgets($fh);
            if (!empty($str)) {
                $arr[] = $str;
                $i++;
                if ($i == $this->num) {
                    echo $i . PHP_EOL;
                    // 批量构造sql
                    $sql = $this->buildSql($arr);
                    print_r($arr);
                    //有bug的地方就是在这里
                    unset($arr);  
                    if (!empty($sql)) {
                        // 提交sql
                        echo $sql;
                    }
                    $i = 0;
                }
            }
        }
    }
    protected function buildSql($arr)
    {
        if (!empty($arr)) {
            $sql = "INSERT INTO {$this->table} VALUES ";
            foreach ($arr as $key => $value) {
                $value = trim($value);
                $sql .= "('column1','$value','column3')";
                if ($key != count($arr) - 1) {
                    $sql .= ',';
                }
            }
        }
        return $sql;
    }
}
$a = new batchInsertController();
$a->batchInsert();

这次就对了,$arr 数组没有被unset 掉,结果一直往数组里面写入元素,这也是会造成内存泄漏的地方

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
INSERT INTO test.t VALUES ('column1','1','column3'),('column1','2','column3'),('column1','3','column3')3
Array
(
    [0] => 4
    [1] => 5
    [2] => 6
)
INSERT INTO test.t VALUES ('column1','4','column3'),('column1','5','column3'),('column1','6','column3')3
Array
(
    [0] => 7
    [1] => 8
    [2] => 9
)
INSERT INTO test.t VALUES ('column1','7','column3'),('column1','8','column3'),('column1','9','column3')

这就完了,并没有,文件中是10条记录,你这里只处理了9条,剩下一条不能丢掉呀
最后执行完批量以后,如果$arr 数组中还有元素,将剩余元素也写入到数据库中

<?php
class batchInsertController {
    private $num = 3;
    private $table = 'test.t';
    public function batchInsert()
    {
        // read file
        ini_set('memory_limit', -1);
        $fh = fopen("./test.txt", "r");
        $i = 0;
        while (!feof($fh)) {
            $str = fgets($fh);
            if (!empty($str)) {
                $arr[] = $str;
                $i++;
                if ($i == $this->num) {
                    echo $i . PHP_EOL;
                    // 批量构造sql
                    $sql = $this->buildSql($arr);
                    print_r($arr);
                    // 有bug的地方
                    unset($arr);
                    if (!empty($sql)) {
                        // 提交sql
                        echo $sql;
                    }
                    $i = 0;
                }
            }
        }
        if (!empty($arr)) {
            $sql = $this->buildSql($arr);
            print_r($arr);
            unset($arr);
            if (!empty($sql)) {
                // 提交sql
                echo $sql;
            }
        }
    }
    protected function buildSql($arr)
    {
        if (!empty($arr)) {
            $sql = "INSERT INTO {$this->table} VALUES ";
            foreach ($arr as $key => $value) {
                $value = trim($value);
                $sql .= "('column1','$value','column3')";
                if ($key != count($arr) - 1) {
                    $sql .= ',';
                }
            }
        }
        return $sql;
    }
}
$a = new batchInsertController();
$a->batchInsert();

总结:

  1. 以前一直觉得php简单,发现自己用php写个小程序bug都很多,原来不是php简单,是用php的人"简单",推而广之,无论你用那种语言,如果不持续学习和主动学习,你还是写不出好的代码(优雅,可扩展,可维护,可读性高)

  2. 越来越明白基础扎实是有多么重要了,有扎实的数据结构和算法知识,才能更好更快的解决业务中遇到的问题

  3. 写代码前要思考和设计,然后要动手写,写完以后看下有没有可以改进和优化的,这个过程没有捷径,只有不断练习和学习,才能有所长进

posted @ 2020-11-03 18:03  Paualf  阅读(330)  评论(0编辑  收藏  举报