after the database design is complete, it is often necessary to keep a desc description of all tables in the database in a wiki or other document, in particular the meaning and purpose of each field. It is not advisable to manually generate the nature. Therefore, I have written a simple Python program that automatically generates DESC descriptions of all the tables in the database and outputs them in a readable format.
#-*-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 pythonImportMysqldbglobalfielddescs= ('Field','Type','Null','Key','Default','Extra') Globaldescfile='Desc.txt'Conflictedwithmysqlkeywords= Set (['Group']) fielddescmapping= { 'ID':'uniquely identifies', 'is_deleted':'whether to delete tombstone', 'Status':'Entity Status', 'type':'entity Type', ' Priority':'Priority Level', 'Password':'Password', 'IP':'IP Address', 'mac':'MAC address', 'Protocol':'Access Agreement', 'user_id':'User Unique identification'}classDB (object):def __init__(self): Self.conn= MySQLdb.connect (db='MySQL', host='127.0.0.1', user='Root', passwd='123456') defobtaindb (self):return Selfdefquery (self, SQL): Cursor=self.conn.cursor () cursor.execute (SQL) result=Cursor.fetchall () cursor.close ( )returnlist (Result)defFormatcols (FIELDDESC):return "%-16s%-24s%-5s%-8s%-8s%-30s"%FielddescdefWithnewline (ASTR):returnAstr +'\ n'defcommonfieldsprocess (fielddesclist): FieldName=Fielddesclist[0] Fielddesc=fielddescmapping.get (fieldName) Desclen=Len (fielddesclist)ifFielddesc isNone:ifFieldname.startswith ('Gmt_c'): Fielddesc='creation Time' elifFieldname.startswith ('gmt_m'): Fielddesc='Modification Time' Else: Fielddesc= Fielddesclist[desclen-1] Fielddesclist[desclen-1] =FielddescdefFORMATF (fielddesctuple): Fielddesclist=list (fielddesctuple) Fieldlen=Len (fielddesclist) forIinchRange (Fieldlen):ifFielddesclist[i] isNone:fielddesclist[i]='NULL' Else: Fielddesclist[i]=str (fielddesclist[i]) commonfieldsprocess (fielddesclist)returnformatcols (Tuple (fielddesclist))defformat (TABLEDESC): Desc="' forFielddesctupleinchTabledesc:desc+=Withnewline (FORMATF (fielddesctuple))returndescdefdescdb (givendb): Tablesret= Givendb.query ("show tables;") PrintTablesret tablenames= [Table[0] forTableinchTablesret] desc="' forTableNameinchTablenames:ifTableNameinchConflictedwithmysqlkeywords: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') returndescdefMain (): Descfile= Open (Globaldescfile,'W') DB=DB () database=db.obtaindb () desc=descdb (database) Descfile.write (Desc.encode ('Utf-8') ) Descfile.close ()if __name__=='__main__': Main ()
Desc Description of all tables in the Python build database