zoukankan      html  css  js  c++  java
  • 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('
    ')
        f.close()
        print('generate markdown success!')
    
  • 相关阅读:
    STL的相关知识
    有关欧拉通路/回路的一些资料整理
    差分约束
    BZOJ 2100: [Usaco2010 Dec]Apple Delivery
    BZOJ 2017: [Usaco2009 Nov]硬币游戏(A Coin Game)
    vijos 1282&1283&1284&1285 佳佳的魔法照片/魔法药水/魔杖/魔法阵
    BZOJ 1660: [Usaco2006 Nov]Bad Hair Day
    BZOJ 1602: [Usaco2008 Oct]牧场行走
    BZOJ 1647: [Usaco2007 Open]Fliptile 翻格子游戏
    BZOJ 1646: [Usaco2007 Open]Catch That Cow
  • 原文地址:https://www.cnblogs.com/jiangchunsheng/p/13405601.html
Copyright © 2011-2022 走看看