吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2631|回复: 9
收起左侧

[其他转载] mysql数据库数据字典生成

[复制链接]
redblue 发表于 2020-12-5 21:50

mysql 数据库数据字典生成

<?php
// -h host  -u user  -p password -P port -c charset -d dbname 
$param = getopt('h:u:p:P:c:d:');
$host = $param['h'] ?? '127.0.0.1';
$port = $param['P'] ?? 3306;
$user = $param['u'] ?? 'root';
$pass = $param['p'] ?? '123456';
$charset = $param['c'] ?? 'utf8';
$dbname = $param['d'] ?? '';

$dsn = "mysql:dbname={$dbname};host={$host};port={$port}";
try {
    $pdo = new PDO($dsn, $user, $pass, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"]);
} catch (PDOException $e) {
    echo 'Connection failed:' . $e->getMessage();
}

//获取数据库中所有表信息
$sql = "SHOW TABLE STATUS FROM {$dbname}";
$result = $pdo->query($sql, PDO::FETCH_ASSOC);
$tables = $result->fetchAll();

$table_count = count($tables);
$html =  '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>' . $dbname . '--数据字典</title>
<style type="text/css">
    table caption, table th, table td {
        padding: 0.1em 0.5em 0.1em 0.5em;
        margin: 0.1em;
        vertical-align: top;
    }
    th {
        font-weight: bold;
        color: black;
        background: #D3DCE3;
    }
    table tr.odd th, .odd {
        background: #E5E5E5;
    }
    table tr.even th, .even {
        background: #f3f3f3;
    }
    .db_table{
        border-top:1px solid #333;
    }
    .title{font-weight:bold;}
</style>
</head>
<body>
<div style="text-align:center;background:#D3DCE3;font-size:19px;">
    <b>' . $dbname . '--数据字典</b>
</div>
<div style="background:#f3f3f3;text-align:center;">(注:共' . $table_count . '张表,按ctrl+F查找关键字)</div>' . "\n";
for ($i = 0; $i < $table_count; $i++) {
    $html .= '<ul type="square">' . "\n";
    $html .= '  <li class="title">';
    $html .= ($i + 1) . '、表名:[' . $tables[$i]['Name'] . ']      注释:' . $tables[$i]['Comment'];
    $html .= '</li>' . "\n";
    //查询数据库表字段信息
    $tab_name = $tables[$i]['Name'];
    $sql_tab = 'SHOW FULL FIELDS FROM `' . $tables[$i]['Name'] . '`';
    $result = $pdo->query($sql_tab, PDO::FETCH_ASSOC);
    $field_info = $result->fetchAll();

    $html .= '<li style="list-style: none outside none;"><table border="0" class="db_table" >';
    $html .= '<tr class="head">
        <th style="width:110px">字段</th>
        <th>类型</th>
        <th>为空</th>
        <th>额外</th>
        <th>默认</th>
        <th style="width:95px">字符集</th>
        <th>是否主键</th>
        <th>备注</th></tr>';
    for ($j = 0; $j < count($field_info); $j++) {
        $html .= '        <tr class="' . ($j % 2 == 0 ? "odd" : "even") . '">' . "\n";
        $html .= '          <td>' . $field_info[$j]['Field'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Type'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Null'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Extra'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Default'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Collation'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Key'] . '</td>' . "\n";
        $html .= '          <td>' . $field_info[$j]['Comment']. '</td>' . "\n";
        $html .= '        </tr>' . "\n";
    }
    $html .= '  </table></li>' . "\n";
    $html .= '</ul>' . "\n";
}
$html .= '</body>' . "\n";
$html .= '</html>' . "\n";

file_put_contents($dbname .'.html', $html);

使用方法:php dict.php -h 127.0.0.1 -u root -p 123456 -P 3306 -d blog_service
参数说明:-h mysql服务器地址 -u mysql用户名 -p mysql密码 -P mysql端口 -d mysql数据库名
参数都有默认值,-d参数必传 最简单的用法:php dict.php -d blog_service
执行命令后生成一个以数据库名命名的html文件

免费评分

参与人数 3吾爱币 +2 热心值 +2 收起 理由
THMZ + 1 谢谢@Thanks!
zecore + 1 + 1 谢谢@Thanks!
zzl6688 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

吴书醉 发表于 2020-12-5 22:11
大佬字典的生成还是需要注释啊
tinglie 发表于 2020-12-5 22:15
刚试了一下,取数据库中所有表的描述信息并显示出来,适合一小部分场景,如果表多排序不美观,后续可优化。不过还是 naivcat 好使
tinglie 发表于 2020-12-5 22:21
收藏了,丢码云的片段上,日后当个CV工程师,哈哈哈谢谢分享
zecore 发表于 2020-12-6 00:03
好东西,收藏了
OO2OO 发表于 2020-12-6 07:24
感谢分享
红星牌拖拉机 发表于 2020-12-6 10:48
挺好的,感谢楼主分享
 楼主| redblue 发表于 2020-12-6 12:42
tinglie 发表于 2020-12-5 22:15
刚试了一下,取数据库中所有表的描述信息并显示出来,适合一小部分场景,如果表多排序不美观,后续可优化。 ...

这个只是为了,项目开发过程中生成数据字典,比如说你修改了数据表,直接一键导出数据库的信息。而不用再去维护数据字典了。
yellowprinting 发表于 2020-12-6 16:31
谢谢分享
wxllv 发表于 2020-12-15 10:03
有没有sql server的?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-7 07:41

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表