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)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 芦溪县| 随州市| 调兵山市| 昌乐县| 汝南县| 徐州市| 营山县| 凌源市| 新泰市| 融水| 建阳市| 普安县| 望谟县| 色达县| 安岳县| 珲春市| 岳西县| 乌拉特前旗| 博白县| 柘荣县| 依兰县| 伊宁县| 平果县| 阜宁县| 加查县| 惠州市| 宜丰县| 于田县| 保康县| 滦平县| 普宁市| 南康市| 名山县| 墨玉县| 咸宁市| 新田县| 同德县| 阳原县| 宁国市| 闽侯县| 虹口区|