[原创]通用数据库分服[

来源:互联网 发布:mongodb mysql 区别 编辑:程序博客网 时间:2024/04/29 16:39

function InsertValuesStr($Rs){
 $InsertValuesStr="";
 while($Row=$Rs->FetchRow()){
  $InsertRowStr="";
  for ($i=0;$i<count($Row)/2;$i++){$InsertRowStr.= ",'".$Row[$i]."'";}
  $InsertRowStr=substr($InsertRowStr,1);
  $InsertValuesStr.=",(".$InsertRowStr.")";
 }
 if ($InsertValuesStr!="") $InsertValuesStr=substr($InsertValuesStr,1);
 return $InsertValuesStr;
}
function BackupDataBase($MainConn,$SubConn,$TableName,$WhereStr="",$Type="REPLACE"){

 if ($TableName=="") return "";

 $MainSql="SELECT * FROM $TableName";
 if ($WhereStr!="") $MainSql.=" where ".$WhereStr;
 $MainRs=$MainConn->Execute($MainSql);
 if (!$MainRs) return "";
 if ($MainRs->RecordCount()>0){
  $ItemsValueStr=InsertValuesStr($MainRs);
  $NewItemsSql="";
  $Type=strtoupper($Type);
  if ($Type=="INSERT") $SqlType="INSERT INTO ";
  elseif ($Type=="REPLACE") $SqlType="REPLACE INTO ";
  else return "";
  $NewItemsSql="$SqlType $TableName values ". $ItemsValueStr.";";
  $SubConn->Execute($NewItemsSql);
  echo $SubConn->ErrorMsg();
  return "导出表“".$TableName."”成功!<br>";
 }else{
  return "导出表“".$TableName."”中无数据!<br>";
 }
 
}

 

程序说明:
调用函数
BackupDataBase
第一个参数据为原数据库链接
第二个参数为目标数据库库链接。
第三个参数为表名
第四个参数为导出条件
第五个参数为新增还是替换
本例中使用的是adodb
其实本例就是前两天我提出的一个问题就是
insert into tb1 select * from tb2的替代程序。
有意见的请发表,本人的程序水平不限,若有更加好的方法,请贴出代码,谢谢。
另本贴强烈要求加精。

 

 

我的调用方法。   

$TableArray=array("items","players","pvp2","quests","recipes","skills","talents","talenttree","members");
   $Sql="select * from members  where member_id=".$MemberID;
   $Rs=$SubConn->Execute($Sql);
   if ($Rs){
    if ($Rs->RecordCount()<1){
     $WhereStr=" member_id=".$MemberID;
     $Message="";
     foreach($TableArray as $TableName){
      $Message.= BackupDataBase($MainConn,$SubConn,$TableName,$WhereStr,"REPLACE");
     }
     
    }else{
     $Message="<br>记录已经存在,此次执行跳过!";
    }
    }else{
     echo $SubConn->ErrorMsg();
    } 
   $SubConn->Close();
$TableArray中是需要导出的表。

首先判断目标库中有没有此导出的记录。然后一并导出与该用户有关的所有资料。

这里不需要知道表中的结构,只要知道表的关联字段就可以了。

 

 

原创粉丝点击