首页 > 代码库 > python生成数据库中所有表的DESC描述

python生成数据库中所有表的DESC描述

 

       在数据库设计完成之后, 常常需要在 wiki 或其他文档中保存一份数据库中所有表的 desc 描述, 尤其是每个字段的含义和用途。 手动去生成自然是不可取的。 因此, 我编写了一个简单的 python 程序,可以自动生成数据库中所有表的 desc 描述, 并以可读格式输出。

 

# -*- coding: utf-8 -*-# -------------------------------------------------------------------------------# Name:          db_tables_descs.py# Purpose:       generate the tables that describe the meanings of fields in db## Author:       qin.shuq## Created:      2014/11/17# Output:       desc.txt#               recording the tables that describe the meanings of fields in db#-------------------------------------------------------------------------------#!/usr/bin/env pythonimport MySQLdbglobalFieldDescs = (Field, Type, Null, Key, Default, Extra)globalDescFile = desc.txtconflictedWithMysqlKeywords = set([group])fieldDescMapping = {    id:         唯一标识,    is_deleted: 是否逻辑删除,    status:     实体状态,    type:       实体类型,    priority:   优先级,    password:   密码,    ip:         ip 地址,    mac:        mac 地址,    protocol:   访问协议,    user_id:    用户唯一标识}class DB(object):    def __init__(self):        self.conn = MySQLdb.connect(db=mysql,host=127.0.0.1,user=root,passwd=123456)    def obtainDB(self):        return self    def query(self, sql):        cursor = self.conn.cursor()        cursor.execute(sql)        result =  cursor.fetchall()        cursor.close()        return list(result)def formatCols(fieldDesc):    return  "%-16s %-24s %-5s %-8s %-8s %-30s" % fieldDescdef withNewLine(astr):    return astr + \ndef commonFieldsProcess(fieldDescList):    fieldName = fieldDescList[0]    fieldDesc = fieldDescMapping.get(fieldName)    desclen =   len(fieldDescList)    if fieldDesc is None:        if fieldName.startswith(gmt_c):            fieldDesc = 创建时间        elif fieldName.startswith(gmt_m):            fieldDesc = 修改时间        else:            fieldDesc = fieldDescList[desclen-1]    fieldDescList[desclen-1] = fieldDescdef formatF(fieldDescTuple):    fieldDescList = list(fieldDescTuple)    fieldLen = len(fieldDescList)    for i in range(fieldLen):        if fieldDescList[i] is None:            fieldDescList[i] = NULL        else:            fieldDescList[i] = str(fieldDescList[i])    commonFieldsProcess(fieldDescList)    return formatCols(tuple(fieldDescList))def format(tableDesc):    desc = ‘‘    for fieldDescTuple in tableDesc:        desc += withNewLine(formatF(fieldDescTuple))    return descdef descDb(givenDb):    tablesRet = givenDb.query("show tables;")    print tablesRet    tableNames = [table[0] for table in tablesRet]    desc = ‘‘    for tablename in tableNames:        if tablename in conflictedWithMysqlKeywords:            tablename = ` + tablename + `        descSql = "desc " + tablename        tableDesc = givenDb.query(descSql)        desc += withNewLine(tablename)        desc += withNewLine(formatCols(globalFieldDescs)).decode(utf-8)        desc += withNewLine(format(tableDesc)).decode(utf-8)        desc += withNewLine(‘‘).decode(utf-8)    return descdef main():    descFile = open(globalDescFile, w)    db = DB()    database = db.obtainDB()    desc = descDb(database)    descFile.write(desc.encode(utf-8))    descFile.close()if __name__ == __main__:    main()

 

python生成数据库中所有表的DESC描述