sql_split($tableSql, "fox_"); // 执行SQL语句 try { // 执行SQL语句 $counts = count($sqlFormat); for ($i = 0; $i < $counts; $i++) { $sql = trim($sqlFormat[$i]); if (stristr($sql, 'CREATE TABLE')) { Db::execute($sql); } else { if (trim($sql) == '') continue; Db::execute($sql); } } return true; } catch (\Exception $e) { return false; } } // 删除表 public static function del_table($table) { $table = str_replace("`", "", $table); try { $sql = "DROP TABLE `{$table}`"; Db::execute($sql); return 1; } catch (\Exception $e) { return 0; } } // 判断表是否存在 public static function check_table($table) { $table = str_replace("`", "", $table); $res = Db::query('SHOW TABLES LIKE ' . "'" . $table . "'"); if ($res) { return 1; } else { return 0; } } /** * 判断字段是否存在 * @param $table 表名 * @param $column 字段 */ public static function check_column($table, $column) { $table = str_replace("`", "", $table); $res = Db::query('select count(*) from information_schema.columns where table_name = ' . "'" . $table . "' " . 'and column_name =' . "'" . $column . "'"); if ($res[0]['count(*)'] != 0) { return 1; } else { return 0; } } /** * 添加表字段 * @param $table 表名 * @param $column 字段 * @param $type 类型如 varchar(255) * @param $condition 条件如 DEFAULT NULL或 NOT NULL * @param $after 某个字段后 */ public static function add_column($table, $column, $type, $condition, $after) { $table = str_replace("`", "", $table); try { Db::execute('alter table' . " `" . $table . "` " . 'add' . " `" . $column . "` " . $type . " " . $condition . " " . 'after' . " `" . $after . "`"); return 1; } catch (\Exception $e) { return 0; } } /** * 删除表字段 * @param $table 表名 * @param $column 字段 */ public static function del_column($table, $column) { $table = str_replace("`", "", $table); try { Db::execute('alter table ' . "`" . $table . "`" . ' drop column' . "`" . $column . "`"); return 1; } catch (\Exception $e) { return 0; } } /** * 修改表字段 * @param $table 表名 * @param $old_column 要修改字段 * @param $column 新字段 * @param $type 字段类型如 varchar(250) */ public static function update_column($table, $old_column, $column, $type) { $table = str_replace("`", "", $table); //字段名和类型同时修改才会返回1不然返回0 try { Db::execute('alter table' . " `" . $table . "` " . 'change' . " `" . $old_column . "` " . "`" . $column . "`" . $type); return 1; } catch (\Exception $e) { return 0; } } // 拆分sql内容 public static function sql_split($sql, $tablepre = "fox_") { if ($tablepre != "fox_") $sql = str_replace("`fox_", '`' . $tablepre, $sql); $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql); $sql = str_replace("\r", "\n", $sql); $ret = array(); $num = 0; $queriesarray = explode(";\n", trim($sql)); unset($sql); foreach ($queriesarray as $query) { $query = remove_str_bom($query); $ret[$num] = ''; $queries = explode("\n", trim($query)); $queries = array_filter($queries); foreach ($queries as $query) { $str1 = substr($query, 0, 1); if ($str1 != '#' && $str1 != '-') $ret[$num] .= $query; } $num++; } return $ret; } }