python 3.7 生成数据库文档

Posted lobin.tong

tags:

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

开发阶段数据库总是有变动,开发人员需要维护文档给相关人员使用,故编写一个脚本自动生成数据库文档

生成的excel如下

 

 

 

 

 

import cx_Oracle
import os
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import PatternFill

os.environ[\'NLS_LANG\'] = \'SIMPLIFIED CHINESE_CHINA.UTF8\'

tableSql = "select * from user_tab_comments"

tableColumnSql = "SELECT CC.column_name,CC.data_type,CC.data_length,CC.default_length,CM.comments " \\
                 "FROM USER_TAB_COLUMNS cc INNER JOIN USER_COL_COMMENTS cm ON cc.column_name = cm.column_name  " \\
                 "WHERE CC.table_name =\'%s\' AND CM.table_name =\'%s\' ORDER BY CC.column_id"
\'\'\'
保存数据字典到指定./space目录下
space->环境(test,uat,pro)
usr->用户名
pwd->密码
host->主机名
sid->sid
\'\'\'

def saveDbSchema(space, usr, pwd, host, sid):
    isExsit = os.path.exists(space)
    if not isExsit:
        os.makedirs(space)
        print("create dir->%s" % (space))

    constr = "%s/%s@%s/%s" % (usr, pwd, host, sid)
    print(constr)
    conn = cx_Oracle.connect(constr)
    cursor = conn.cursor()

    cursor.execute(tableSql)

    datas = cursor.fetchall()

    wb = Workbook()
    # load_workbook("mergeData.xlsx")
    title = ["字段名", "字段类型", "字段长度", "默认值", "备注"]
    ws_inx = wb["Sheet"]
    ws_inx.append(["表名", "备注", "链接"])
    ws_inx.column_dimensions[\'A\'].width = 30.0
    ws_inx.column_dimensions[\'B\'].width = 25.0
    ws_inx.column_dimensions[\'C\'].width = 10.0
    for item in datas:
        sql = tableColumnSql % (item[0], item[0])
        cursor.execute(sql)
        data = cursor.fetchall()
        ws = wb.create_sheet(item[0])
        ws.column_dimensions[\'A\'].width = 20.0
        ws.column_dimensions[\'B\'].width = 12.0
        ws.column_dimensions[\'C\'].width = 10.0
        ws.column_dimensions[\'D\'].width = 20.0
        ws.column_dimensions[\'E\'].width = 30.0
        ws.append(["表名", item[2], None, "返回"])
        ws.cell(1, 4).hyperlink = "#Sheet!A1"
        ws.cell(1, 4).fill = PatternFill("solid", fgColor="1874CD")
        ws.append(title)
        for inx, val in enumerate(data):
            ws.append([val[0], val[1], val[2], val[3], val[4]])
        ws_inx.append([item[0], str(item[2]), "链接"])
        ws_inx.cell(ws_inx.max_row, ws_inx.max_column).hyperlink = "#\'%s\'!A1" % (item[0])
    wb.save("./%s/%s.xlsx" % (space, usr))

    print("ok")

 

以上是关于python 3.7 生成数据库文档的主要内容,如果未能解决你的问题,请参考以下文章

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]

CakePHP 3.7。*迁移:跳过或更改schema.lock文件生成路径

❤️用Python+turtle模块实现生成皮卡丘❤️(附源码)

在Python 3.7(macOS)上运行扭曲的示例脚本会引发异常

python 3.7 识别图片

Python代码阅读(第40篇):通过两个列表生成字典