php 之批量生成 mysql 语句 注释

工作需要 需要更新 所有表的注释 不影响原结构之上进行更新

<?php 


class Ceshi extends CI_Controller{

    function __construct()
    {
         parent::__construct();

        /* 加载项 */
        $this->load->model('Convers', 'conv');

      
    }

    /**
     


     /**
     * 获取所有数据
     */
    public function ceshi(){

        //获取所有的账户信息
        $result=$this->conv->getAdmininfo();
        //获取所有的专家信息
        $result11=$this->conv->getPart11();

        //循环账户信息
        //获取账户的uid和名称 
        //将名称和专家的单位想比较 然后进行替换uid
        foreach($result as $val){

            

            foreach($result11 as $val1){
                //这里更新的是 某些数据没有单位 是以id显示的 所以统一更新为单位
                // if($val1['danweimingcheng_12']!==$val1['xuhao_12']){

                //     $a=$this->conv->SaveName($val1['id'],$val1['danweimingcheng_12']);
                //     if($a>0){
                //         echo "更新成功单位为".$val1['danweimingcheng_12']."</br>";
                //     }
                // }else{
                //     continue;
                // }

                if($val['admin_name']==$val1['xuhao_12']){
                    $a=$this->conv->SaveUid($val1['id'],$val['admin_id']);
                    if($a>0){
                        echo "更新成功ID为".$val1['id']."</br>";
                    }
                }else{
                    continue;
                }

            }
        }

       
        

    }

    public function uptijiao(){
         //省厅提交 学校提交
         $result11=$this->conv->getPart11();

         $result=$this->conv->getAdmininfo();

         //学校提交为1; xx_tijiao
         //省厅提交为1 st_tijiao
         foreach($result as $val){

            

            foreach($result11 as $val1){

               if($val1['uid']==$val['admin_id']){
                   $data['xx_tijiao']=1;
                   $data['st_tijiao']=1;
                   $a=$this->conv->SaveST($val1['id'],$data);
                   if($a>0){
                       echo "更新成功ID为".$val1['id']."</br>";
                   }


               }else{
                   continue;
               }

            }
        }
    }

    //获取表的注释:
    public function getdescs(){
        //获取表结构
        $talbe=$this->conv->getTable();
        foreach($talbe as $v){
            echo "--当前表:".$v."----</br>";
            //var_dump($this->conv->getDesc($v));
            $ceshi=$this->conv->getDesc($v);
            //循环查询表的字段信息
            
            foreach($ceshi as $val){

              
               // $this->AlterContent($v,$val);
            
              
               if($val['Collation']=="utf8_general_ci"){
                   
                    $where="CHARACTER SET utf8 COLLATE {$val['Collation']}";
               }else{
                     $where="";
               }

               if($val['Null']=="YES"){
                    $nus="NULL";
               }else{
                    $nus="NOT NULL";
               }

               if($val['Default']=="NULL"){
                    $defaul=" ";
               }else{
                    $defaul="DEFAULT '{$val['Default']}' ";
               }
               
               echo "ALTER TABLE `{$v}` CHANGE `{$val['Field']}`  `{$val['Field']}`  {$val['Type']} {$where}  {$nus} {$defaul} COMMENT '{$val['Comment']}';"."</br>";

                
            }

        }
    }
    //情况分析第一种
    function AlterContent($v,$val){
        
        //编码不为null 但是 允许空值
        if($val['Collation']=="utf8_general_ci" && $val['Null']=="YES" ){
            echo "ALTER TABLE {$v} CHANGE {$val['Field']}  {$val['Field']}  {$val['Type']} CHARACTER SET utf8 COLLATE {$val['Collation']}  NULL DEFAULT {$val['Default']} COMMENT '{$val['Comment']}';"."</br>";
            
        
        }
        //编码不为null  但是不 允许空值
        if($val['Collation']=="utf8_general_ci" || $val['Null']=="No"){
            echo "ALTER TABLE {$v} CHANGE {$val['Field']} {$val['Field']} {$val['Type']} CHARACTER SET utf8 COLLATE {$val['Collation']} NOT NULL DEFAULT '{$val['Default']}' COMMENT '{$val['Comment']}';"."</br>";

        }

        if($val['Collation']!="utf8_general_ci" || $val['Null']=="YES"){
            echo "ALTER TABLE {$v} CHANGE {$val['Field']} {$val['Field']} {$val['Type']} CHARACTER SET utf8 COLLATE {$val['Collation']} NOT NULL DEFAULT '{$val['Default']}' COMMENT '{$val['Comment']}';"."</br>";

        }

        if($val['Collation']!="utf8_general_ci" || $val['Null']=="No"){
            echo "ALTER TABLE {$v} CHANGE {$val['Field']} {$val['Field']} {$val['Type']} CHARACTER SET utf8 COLLATE {$val['Collation']} NOT NULL DEFAULT '{$val['Default']}' COMMENT '{$val['Comment']}';"."</br>";

        }


    }
}

model

 

<?php
/**
 * @Name      DownLoadModel.php
 * @Note      数据下载模型
 * @Author    caotl
 * @Created   2016-01-05 11:22:14
 * @Version   go!cms 1.0.0
 *
 */
class Convers extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    
    public function getAdmininfo(){

        $result=$this->db->get('t_admin_info')->result_array();

        return $result;

    }

    public function getPart11(){

        $result=$this->db->get('t_part11')->result_array();

        return $result;

    }

    public function SaveUid($id,$uid){
        $row=$this->db->where('id',$id)->update('t_part11',array('uid'=>$uid));

        return $row;
    }
    /**
     * 修改同样的名称
     */
    public function SaveName($id,$name){
        $row=$this->db->where('id',$id)->update('t_part11',array('xuhao_12'=>$name));

        return $row;
    }

    //学校更新省厅更新
    public function SaveST($id,$data){
        $row=$this->db->where('id',$id)->update('t_part11',$data);

        return $row;
    }

    //获取所有的表名称

    public function getTable(){
        $row=$this->db->list_tables();  //获取所有的表数组

        return $row;
    }

    public function getDesc($str=""){
        //$rows="show create table ".$str;
        $rows="SHOW FULL COLUMNS FROM ".$str;
        $query=$this->db->query($rows);
       $result=$query->result_array();
        return $result;
    }





   
}
?>

  

posted @ 2018-07-18 14:23  尘梦  阅读(391)  评论(0编辑  收藏  举报