如何利用PHP执行.SQL文件
2016-07-12 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
代码如下:
<?php
class DBManager
{
var $dbHost = '';
var $dbUser = '';
var $dbPassword = '';
var $dbSchema = '';
var $conn;
function __construct($host,$user,$password,$schema)
{
$this->dbHost = $host;
$this->dbUser = $user;
$this->dbPassword = $password;
$this->dbSchema = $schema;
}
public function executeFromString($sql,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
{
return $this->execute($sql,$delimiter,$prefix ,$commenter);
}
public function executeFromFile($sqlPath,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
{
//判断文件是否存在
if(!file_exists($sqlPath))return false;
$handle = fopen($sqlPath,'rb');
$sqlStr = fread($handle,filesize($sqlPath));
fclose($handle);
return $this->execute($sqlStr,$delimiter,$prefix ,$commenter);
}
protected function execute($sqlStr,$delimiter = '(;\n)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
{
//通过sql语法的语句分割符进行分割
$segment = explode(";",trim($sqlStr));
//var_dump($segment);
//去掉注释和多余的空行
foreach($segment as & $statement):
$sentence = explode("\n",$statement);
$newStatement = array();
foreach($sentence as $subSentence):
if(''!= trim($subSentence)):
//判断是会否是注释
$isComment = false;
foreach($commenter as $comer):
if(eregi("^(".$comer.")",trim($subSentence))):
$isComment = true;
break;
endif;
endforeach;
//如果不是注释,则认为是sql语句
if(!$isComment)
$newStatement[] = $subSentence;
endif;
endforeach;
$statement = $newStatement;
endforeach;
//对表名加前缀
if('' != $prefix)://只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName
$regxTable = "^[\`\'\"]{0,1}[\_a-zA-Z]+[\_a-zA-Z0-9]*[\`\'\"]{0,1}$";//处理表名的正则表达式
$regxLeftWall = "^[\`\'\"]{1}";
$sqlFlagTree = array
(
"CREATE" => array("TABLE" => array("$regxTable" => 0)),
"INSERT" => array("INTO" => array("$regxTable" => 0))
);
foreach($segment as & $statement):
$tokens = split(" ",$statement[0]);
$tableName = array();
$this->findTableName($sqlFlagTree,$tokens,0,$tableName);
if(emptyempty($tableName['leftWall'])):
$newTableName = $prefix.$tableName['name'];
else:
$newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
endif;
$statement[0] = str_replace($tableName['name'],$newTableName,$statement[0]);
endforeach;
endif;
//组合sql语句
foreach($segment as & $statement):
$newStmt = '';
foreach($statement as $sentence):
$newStmt = $newStmt.trim($sentence)."\n";
endforeach;
$statement = $newStmt;
endforeach;
self::saveByQuery($segment);
return true;
}
private function saveByQuery($sqlArray)
{
$this->conn = mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword);
mysql_select_db($this->dbSchema,$this->conn);
foreach($sqlArray as $sql):
mysql_query($sql,$this->conn);
endforeach;
}
public function close()
{
mysql_close($this->conn);
}
private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,$tableName = array())
{
$regxLeftWall = "^[\`\'\"]{1}";
if(count($tokens)<=$tokensKey)
return false;
if('' == trim($tokens[$tokensKey])):
return self::findTableName($sqlFlagTree,$tokens,$tokensKey+1,$tableName);
else:
foreach($sqlFlagTree as $flag => $v):
if(eregi($flag,$tokens[$tokensKey])):
if(0==$v):
$tableName['name'] = $tokens[$tokensKey];
if(eregi($regxLeftWall,$tableName['name'])):
$tableName['leftWall'] = $tableName['name']{0};
endif;
return true;
else:
return self::findTableName($v,$tokens,$tokensKey+1,$tableName);
endif;
endif;
endforeach;
endif;
return false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询