Python 生成數據字典


代碼

import pymysql as mysql
import sys
import getopt


def usage():
    print('help:')
    print('--host db server,default localhost')
    print('--port db port,default 3306')
    print('--user db username,default root')
    print('--password db password,default blank')
    print('--database db name')
    print('--output markdown output file,default current path')


if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h",
                                   ["help", "host=", "port=", "database=", "user=", "password=", "output="])
    except getopt.GetoptError:
        sys.exit()
    if 'help' in args:
        usage()
        sys.exit()
        print(opts)
    host = "localhost"
    user = input("帳號:")
    password = input("密碼:")
    database = input("數據庫名:")
    port = 3306
    mdfile = input("請輸入生成的路徑和文件名,默認當前目錄下(./markdown.md):")
    output = mdfile if mdfile else "./markdown.md"

    for op, value in opts:
        if op == '--host':
            host = value
        elif op == '--port':
            port = value
        elif op == '--database':
            database = value
        elif op == '--user':
            user = value
        elif op == '--password':
            password = value
        elif op == '--output':
            output = value
        elif op == '-h':
            usage()
            sys.exit()
        if database == '':
            usage()
            #    sys.exit()
    conn = mysql.connect(host=host, port=port, user=user, password=password, database='information_schema')
    cursor = conn.cursor()
    cursor.execute(
        "select table_name,table_comment from information_schema.tables where table_schema='%s' and table_type='base table'" % database)
    tables = cursor.fetchall()

    markdown_table_header = """

### %s 
#### %s 
字段名 | 字段類型 | 默認值 | 注釋
---- | ---- | ---- | ---- 
"""
    markdown_table_row = """%s | %s | %s | %s
"""
    f = open(output, 'w')
    for table in tables:
        cursor.execute(
            "select COLUMN_NAME,COLUMN_TYPE,COLUMN_DEFAULT,COLUMN_COMMENT from information_schema.COLUMNS where table_schema='%s' and table_name='%s'" % (
            database, table[0]))
        tmp_table = cursor.fetchall()
        p = markdown_table_header % table
        for col in tmp_table:
            p += markdown_table_row % col
        f.writelines(p)
        f.writelines('\r\n')
    f.close()
    print('generate markdown success!')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM