zoukankan      html  css  js  c++  java
  • PDO处理大批量数据的入库

    <?php
    //最东端 东经135度2分30秒
    //最西端 东经73度40分
    //最南端 北纬3度52分
    //最北端 北纬53度33分
    //转换为度的单位


    $dbName = 'txhl';//数据库名称
    $tableName = 'ah';//数据表名称
    $username = 'root';//用户名
    $password = 'root';//密码
    $num = 100000;//规定入库数据条数
    try
    {
    $conn = new PDO('mysql:host=<你要用到的数据库IP地址>;dbname='.$dbName,$username,$password,array(PDO::ATTR_PERSISTENT,true));
    $result = $conn->exec("truncate {$tableName}");

    if($result == false && $conn->errorInfo()[0] != 00000)
    {
    throw new PDOException("Error :".$conn->errorInfo()[2]);
    }
    set_time_limit(200);
    for($i=0;$i<$num;$i++){
    $east = 135+20.30/60; 
    $west = 73+40/60;
    $south = 3+53/60;
    $north = 53+33/60;
    $du = rand(36,38);//经度 75 135 (10份 6度/)lng
    $wei = rand(117,122);//纬度 18 51 (10份 3度/)lat
    $fen = rand(100000,999999);
    $fen1 = rand(100000,999999);
    $latlng = $du.'.'.$fen;
    $lnglat = $wei.'.'.$fen1;
    $conn->exec("insert into $tablename(lat,lng) values($latlng,$lnglat)");
    echo time()."插入成功,自增id为".$conn->lastInsertId().PHP_EOL;

    }
    ignore_user_abort(); //终止脚本的执行
    }catch(PDOException $e)//否则抛出异常
    {
    echo $e->getMessage().PHP_EOL; exit;
    }catch(Exception $e)//否则抛出异常
    {
    echo $e->getMessage().PHP_EOL; exit;
    }

    ?>

  • 相关阅读:
    Go 接口
    Go 参数传递
    Go 结构体
    Go 指针
    使用ContentType处理大量的外键关系
    django的render的特殊用法
    restframework中的那些参数你知道吗?
    scrapy框架
    numpy如何使用
    HTML 5 audio标签
  • 原文地址:https://www.cnblogs.com/findher/p/10610108.html
Copyright © 2011-2022 走看看