生成sql表结构

Posted lgl的守望地盘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成sql表结构相关的知识,希望对你有一定的参考价值。

<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/21
 * Time: 14:26
 * 列子
 * $pdo=new DataConstruct(); 
 * $tables=$pdo->getAllTableNames();
 * $pdo->printLog($pdo->getAllTableConstruct($tables),"F:/logstr.php");
 * $pdo->importSql(‘E:/UserTable.sql‘);
 */

namespace app\index\command;
class DataConstruct
{

    public $dataBaseType;
    public $host;
    public $username;
    public $password;
    public $port;
    public $database;
    public $conn;

    public function __construct($username = "root", $password = "", $database = "aui", $port = "3306", $host = "localhost", $dataBaseType = "mysql")
    {
        $this->dataBaseType = $dataBaseType;
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->port = $port;
        $this->database = $database;
        $dsn = "$dataBaseType:dbname=$database;host=$host";
        try {
            $this->conn = new \PDO($dsn, $this->username, $this->password);

        } catch (PDOException $e) {
            echo ‘Connection failed: ‘ . $e->getMessage();
        }

    }


    /**
     * @desc 获取所有的额表明
     * @return array
     */
    public function getAllTableNames()
    {
        $tableNames = array();
        $sql = "show tables";
        $cursor = $this->conn->query($sql);
        foreach ($cursor as $row) {
            $tableNames[] = $row[0];
        }
        return $tableNames;

    }

    /**
     * @desc 获取某个表的表结构
     * @param $tablename
     * @return array
     */
    public function getTableConstruct($tablename)
    {
        $table = array();
        $sql = "select  COLUMN_NAME,COLUMN_TYPE,COLUMN_DEFAULT,COLUMN_KEY,COLUMN_COMMENT from information_schema.`COLUMNS` where TABLE_NAME=‘" . $tablename . "‘ and TABLE_SCHEMA=‘" . $this->database . "‘";
        $cursor = $this->conn->query($sql, \PDO::FETCH_ASSOC);
        foreach ($cursor as $row) {
            $table[$row[‘COLUMN_NAME‘]] = $row;
        }
        return $table;
    }

    /**
     * @desc 获取所有表的表结构
     * @param array $tableNames
     * @return array
     */
    public function getAllTableConstruct($tableNames = array())
    {
        $AllTableConstruct = array();
        if (is_array($tableNames)) {
            foreach ($tableNames as $tableName) {
                $AllTableConstruct[$tableName] = $this->getTableConstruct($tableName);
            }
        }
        return $AllTableConstruct;
    }


    /**
     * @desc 打印表结构到指定的文件中
     * @param $tableConstruct
     * @param $logpath
     */
    public function printLog($tableConstruct, $logpath)
    {

        foreach ($tableConstruct as $key => $table) {
            file_put_contents($logpath, $key . PHP_EOL, FILE_APPEND);
            file_put_contents($logpath, "COLUMN_NAME   COLUMN_TYPE   COLUMN_DEFAULT   COLUMN_KEY   COLUMN_COMMENT" . PHP_EOL, FILE_APPEND);
            foreach ($table as $column) {
                file_put_contents($logpath, $column[‘COLUMN_NAME‘] . "\t" . $column[‘COLUMN_TYPE‘] . "\t" . $column[‘COLUMN_DEFAULT‘] . "\t" . $column[‘COLUMN_KEY‘] . "\t" . $column[‘COLUMN_COMMENT‘] . PHP_EOL, FILE_APPEND);
            }
            file_put_contents($logpath, PHP_EOL, FILE_APPEND);
        }
    }

    /**
     * @desc 执行sql文件 (不能超温php.ini中配置的内存大小,否则会报内存溢出错误)
     * @param $filepath
     */
    public function importSql($filepath)
    {
        $_sql = file_get_contents($filepath);
        $_arr = explode(‘;‘, $_sql);
        $this->conn->query(‘set names utf8;‘);
        foreach ($_arr as $_value) {
            try {
                $this->conn->query($_value . ‘;‘);
            } catch (\PDOException $e) {
                var_dump($this->conn->errorInfo());
            }

        }
    }
}

 

以上是关于生成sql表结构的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server复制表结构和表数据生成新表的语句

java中如何根据查询出来的表结构自动生成bean

如何从数据库中的表生成树结构?

sql 为给定表生成CREATE TABLE语句(以创建具有相同结构的表)

sql怎么把表结构导出来

怎样导出sql server2000的表结构(不含数据)