SoFunction
Updated on 2024-11-16

Generate mysql database structure documentation with python

Recently, because of project reasons need to write the database design document, but due to too many data tables, manually write the time consuming too long, so get a simple script to quickly generate the database structure, saved to the word document.

Installation of pymysql and document

pip install pymysql
pip install document

scripts

# -*- coding: utf-8 -*-
import pymysql
from docx import Document
from  import Pt
from  import qn

db = (host='127.0.0.1', # Database server IP
                         port=3306,
                         user='root',
                         passwd='123456',
                         db='test_db') # database name)
# Query the corresponding field information based on the table name.
def query(tableName):
    # Open a database connection
    cur = ()
    sql = "select b.COLUMN_NAME,b.COLUMN_TYPE,b.COLUMN_COMMENT from (select * from information_schema.`TABLES`  where TABLE_SCHEMA='test_db') a right join(select * from information_schema.`COLUMNS` where TABLE_SCHEMA='test_db_test') b on a.TABLE_NAME = b.TABLE_NAME where a.TABLE_NAME='" + tableName+"'"
    (sql)
    data = ()
    
    return data
# query the current library below all the table names, table name: tableName; table name + comments (for filling the word document): concat(TABLE_NAME, '(', TABLE_COMMENT, ')')
def queryTableName():
    cur = ()
    sql = "select TABLE_NAME,concat(TABLE_NAME,'(',TABLE_COMMENT,')') from information_schema.`TABLES`  where TABLE_SCHEMA='test_db_test'"
    (sql)
    data = ()
    return data
# Generate word structure for each table, output to word document
def generateWord(singleTableData,document,tableName):
    p=document.add_paragraph()
    p.paragraph_format.line_spacing=1.5 # Set the paragraph to 1.5 times the line spacing.
    p.paragraph_format.space_after=Pt(0) # Set paragraph 0 pounds after paragraph
    #document.add_paragraph(tableName,style='ListBullet')
    r=p.add_run('\n'+tableName)
    =u'Song Style'
    =Pt(12)
    table = document.add_table(rows=len(singleTableData)+1, cols=3,style='Table Grid')
    =Pt(11)
    =u'Calibri'
    # Setting the header style
    # Only three table headers are generated here, which can be modified by actual requirements
    for i in ((0,'NAME'),(1,'TYPE'),(2,'COMMENT')):
        run = (0,i[0]).paragraphs[0].add_run(i[1])
         = 'Calibri'
         = Pt(11)
        r = run._element
        (qn('w:eastAsia'), 'Song Style')
    
    for i in range(len(singleTableData)):
        # Set the style of the data in the table
        for j in range(len(singleTableData[i])):
            run = (i+1,j).paragraphs[0].add_run(singleTableData[i][j])
             = 'Calibri'
             = Pt(11)
            r = run._element
            (qn('w:eastAsia'), 'Song Style')
        #(i+1, 0).text=singleTableData[i][1]
        #(i+1, 1).text=singleTableData[i][2]
        #(i+1, 2).text=singleTableData[i][3]
    

if __name__ == '__main__':
    # Define a document
    document = Document()
    # Setting the default style of fonts
    ['Normal']. = u'Song Style'
    ['Normal']._element.(qn('w:eastAsia'), u'Song Style')
    # Get all table name information and table comment information under the current library
    tableList = queryTableName()
    # Loop through the database to get the table field details and call generateWord to generate word data
    # Due to the rush of time, my side of the choice is to query the database directly, the execution of more than 100 queries, you can optimize, query all the table structure, in the code inside the structure of each table to be split
    for singleTableName in tableList:
        data = query(singleTableName[0])
        generateWord(data,document,singleTableName[1])
    #Save to Document
    ('Database Design.docx')

Preview of the generated word document

在这里插入图片描述

to this article on the use of python to generate mysql database structure document is introduced to this article, more related python to generate mysql structure document content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!