qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          ThinkPHP 數據庫表結構處理類(簡單實用)

          <?php
          /*
          *mysql表結構處理類
          *創建數據表,增加,編輯,刪除表中字段
          *
          */
          classMysqlManage{
          /*
          *創建數據庫,并且主鍵是aid
          *table要查詢的表名
          */
          functioncreateTable($table){
          $sql="CREATETABLEIFNOTEXISTS`$table`(`aid`INTNOTNULLprimarykey)ENGINE=InnoDB;";
          M()->execute($sql);
          $this->checkTable($table);
          }
          /*
          *檢測表是否存在,也可以獲取表中所有字段的信息
          *table要查詢的表名
          *return表里所有字段的信息
          */
          functioncheckTable($table){
          $sql="desc`$table`";
          $info=M()->execute($sql);
          return$info;
          }
          /*
          *檢測字段是否存在,也可以獲取字段信息(只能是一個字段)
          *table表名
          *field字段名
          */
          functioncheckField($table,$field){
          $sql='desc`$table`$field';
          $info=M()->execute($sql);
          return$info;
          }
          /*
          *添加字段
          *table表名
          *info字段信息數組array
          *return字段信息array
          */
          functionaddField($table,$info){
          $sql="altertable`$table`add";
          $sql.=$this->filterFieldInfo();
          M()->execute($sql);
          $this->checkField($table,$info['name']);
          }
          /*
          *修改字段
          *不能修改字段名稱,只能修改
          */
          functioneditField($table,$info){
          $sql="altertable`$table`modify";
          $sql.=$this->filterFieldInfo($info);
          M()->execute($sql);
          $this->checkField($table,$info['name']);
          }
          /*
          *字段信息數組處理,供添加更新字段時候使用
          *info[name]字段名稱
          *info[type]字段類型
          *info[length]字段長度
          *info[isNull]是否為空
          *info['default']字段默認值
          *info['comment']字段備注
          */
          privatefunctionfilterFieldInfo($info){
          if(!is_array($info))
          return
          $newInfo=array();
          $newInfo['name']=$info['name'];
          $newInfo['type']=$info['type'];
          switch($info['type']){
          case'varchar':
          case'char':
          $newInfo['length']=empty($info['length'])?100:$info['length'];
          $newInfo['isNull']=$info['isNull']==1?'NULL':'NOTNULL';
          $newInfo['default']=empty($info['default'])?'':'DEFAULT'.$info['default'];
          $newInfo['comment']=empty($info['comment'])?'':'COMMENT'.$info['comment'];
          case'int':
          $newInfo['length']=empty($info['length'])?7:$info['length'];
          $newInfo['isNull']=$info['isNull']==1?'NULL':'NOTNULL';
          $newInfo['default']=empty($info['default'])?'':'DEFAULT'.$info['default'];
          $newInfo['comment']=empty($info['comment'])?'':'COMMENT'.$info['comment'];
          case'text':
          $newInfo['length']='';
          $newInfo['isNull']=$info['isNull']==1?'NULL':'NOTNULL';
          $newInfo['default']='';
          $newInfo['comment']=empty($info['comment'])?'':'COMMENT'.$info['comment'];
          }
          $sql=$newInfo['name'].''.$newInfo['type'];
          $sql.=(!empty($newInfo['length']))?($newInfo['length'])."":'';
          $sql.=$newInfo['isNull'].'';
          $sql.=$newInfo['default'];
          $sql.=$newInfo['comment'];
          return$sql;
          }
          /*
          *刪除字段
          *如果返回了字段信息則說明刪除失敗,返回false,則為刪除成功
          */
          functiondropField($table,$field){
          $sql="altertable`$table`dropcolumn$field";
          M()->execute($sql);
          $this->checkField($table,$filed);
          }
          /*
          *獲取指定表中指定字段的信息(多字段)
          */
          functiongetFieldInfo($table,$field){
          $info=array();
          if(is_string($field)){
          $this->checkField($table,$field);
          }else{
          foreach($fieldas$v){
          $info[$v]=$this->checkField($table,$v);
          }
          }
          return$info;
          }
          }
              好久沒有寫博客了,最近忙的要死,搞微信平臺,偶爾遇到需要模型管理,前臺表單直接修改表結構的,就自己簡單寫了一下,也不是很難,給大家一個思路

          posted on 2014-08-29 09:49 順其自然EVO 閱讀(475) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 犍为县| 和硕县| 文成县| 孟村| 安岳县| 壤塘县| 高尔夫| 道孚县| 方山县| 大庆市| 桂东县| 曲阳县| 新沂市| 唐河县| 常宁市| 江孜县| 呈贡县| 芦山县| 莱州市| 浙江省| 卢湾区| 长泰县| 通化县| 玛曲县| 安仁县| 昆山市| 北京市| 尉氏县| 朝阳市| 石渠县| 藁城市| 奉节县| 天祝| 绥宁县| 同仁县| 新绛县| 新昌县| 连城县| 莒南县| 镇康县| 民乐县|