/*** 导出sql文件*/public function exportSql(){ //需要导出的数据库表存入到数组当中 $tables =array("T_CRM_QUEUE_FAIL","T_CRM_QUEUE"); //定义一个变量存放sql语句 $content = ‘‘; //打开并写入sql文件 $fp = fopen("./Upload/exportSql/".date(‘Ymd‘).".sql",‘w+‘); foreach ($tables as $k=>$v){ $content .= "\r\n-- ".$v."-- \r\n"; $sql = "SELECT * FROM ".$v; $tablesInfo= M()->query($sql); //将每个表的数据导出到文件 foreach($tablesInfo as $key=> $val){ if($val[‘CDATE‘] != $val[‘UDATE‘]){//如果添加时间和修改时间不一致,则为修改 $sqlInsert = $this->array_to_sql($val,‘update‘); $content.="UPDATE `$v` ".$sqlInsert.";\r\n"; }else{ $sqlInsert = $this->array_to_sql($val); $content.="INSERT INTO `$v` VALUES ".$sqlInsert.";\r\n"; } } } fputs($fp,$content );//写入文件 fclose($fp);//关闭}/** * [array_to_sql 根据数组key和value拼接成需要的sql] * @param [type] $array [key, value结构数组] * @param string $type [sql类型insert,update] * @param array $exclude [排除的字段] * @return [string] [返回拼接好的sql] */public function array_to_sql($array, $type=‘insert‘, $exclude = array()){ $sql = ‘‘; if(count($array) > 0){ foreach ($exclude as $exkey) { unset($array[$exkey]);//剔除不要的key } if(‘insert‘ == $type){ $keys = array_keys($array); $values = array_values($array); $col = implode("`, `", $keys); $val = implode("‘, ‘", $values); $sql = "(`$col`) values(‘$val‘)"; }else if(‘update‘ == $type){ $tempsql = ‘‘; $temparr = array(); foreach ($array as $key => $value) { $tempsql = "‘$key‘ = ‘$value‘"; $temparr[] = $tempsql; } $sql = implode(",", $temparr); } } return $sql;}
时间: 2024-10-20 03:12:25