Desc Description of all tables in the Python build database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.