码迷,mamicode.com
首页 > 其他好文 > 详细

PDO处理大批量数据的入库

时间:2016-11-23 12:28:39      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:单位   roo   persist   sql   atl   root   time   new   数据表   

<?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 eh(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;
}

?>

PDO处理大批量数据的入库

标签:单位   roo   persist   sql   atl   root   time   new   数据表   

原文地址:http://www.cnblogs.com/hopelooking/p/6092968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!