亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Python實現(xiàn)數(shù)據(jù)庫文檔生成工具

 更新時間:2024年04月17日 08:26:15   作者:shigen01  
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)數(shù)據(jù)庫文檔生成工具,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下

逛博客的時候,發(fā)現(xiàn)了一個很有意思的文章:數(shù)據(jù)庫表結(jié)構(gòu)導(dǎo)出工具。帶著好奇,我也去DIY了一個,先看看效果:

這個就是主要的程序界面,可以選擇生成md文檔或者docx文檔。具體的文檔內(nèi)容如下:

md文檔

docx文檔

功能可以成功的實現(xiàn),現(xiàn)在我們來分析一下原理,核心就是幾條sql語句:

 use security;
 ?
 show tables;
 ?
 desc dict;
 show full columns from dict where field = 'is_del';

原理分析

查看全部的表

使用show tables就可以看到當(dāng)前數(shù)據(jù)庫下邊的所有的數(shù)據(jù)表。

查看表結(jié)構(gòu)

使用desc xxx即可查看表的詳細結(jié)構(gòu),包含字段的全部信息。

查看字段注釋

字段的注釋需要額外的sql了:

當(dāng)然,shigen也看到過連表查詢出字段的注釋的,可以進一步的改進。

總結(jié)

做一波小總結(jié):其實數(shù)據(jù)庫文檔生成,就是用一些sql語句獲得對應(yīng)的表信息和字段信息,使用文本拼接寫入到文件里。

代碼分享

那本次設(shè)計的代碼也在這里,歡迎大家學(xué)習(xí)交流:

 from tkinter import *
 from tkinter import messagebox
 ?
 import mysql.connector
 from docx import Document
 from tabulate import tabulate
 ?
 ?
 # 連接到MySQL數(shù)據(jù)庫
 def connect_to_database():
     host = host_entry.get()
     user = user_entry.get()
     password = password_entry.get()
     database = database_entry.get()
 ?
     try:
         conn = mysql.connector.connect(host=host,
                                        port=3306,
                                        user=user,
                                        password=password,
                                        database=database)
         return conn
     except mysql.connector.Error as err:
         messagebox.showerror("錯誤", f"連接到MySQL數(shù)據(jù)庫時出錯:{err}")
         return None
 ?
 ?
 # 獲取數(shù)據(jù)庫中的表信息及字段注釋
 def get_table_info(conn):
     tables_info = []
     if conn:
         cursor = conn.cursor()
         cursor.execute("SHOW TABLES")
         tables = cursor.fetchall()
         for table in tables:
             table_name = table[0]
             cursor.execute(f"DESCRIBE {table_name}")
             table_structure = cursor.fetchall()
             tables_info.append({
                 "table_name": table_name,
                 "structure": table_structure
             })
         cursor.close()
     return tables_info
 ?
 ?
 # 獲取字段注釋
 def get_field_comment(table_name, field_name):
     cursor = conn.cursor()
     cursor.execute(
         f"SHOW FULL COLUMNS FROM {table_name} WHERE Field = '{field_name}'")
     column_info = cursor.fetchone()
     comment = column_info[8]  # 注釋信息在第9個元素中
     cursor.close()
     return comment
 ?
 ?
 # 生成Markdown格式的數(shù)據(jù)庫文檔
 def generate_markdown_documentation(tables_info):
     documentation = "# 數(shù)據(jù)庫文檔\n\n"
     documentation += f"數(shù)據(jù)庫地址:{host_entry.get()}\n"
     documentation += f"用戶名:{user_entry.get()}\n"
     documentation += f"數(shù)據(jù)庫名稱:{database_entry.get()}\n\n"
     for table_info in tables_info:
         table_name = table_info["table_name"]
         structure = table_info["structure"]
         documentation += f"## {table_name}\n\n"
         headers = ["字段", "類型", "允許空值", "鍵", "默認值", "額外信息", "注釋"]  # 添加注釋列
         rows = []
         for field_info in structure:
             rows.append(
                 list(field_info) +
                 [get_field_comment(table_name, field_info[0])])  # 獲取字段注釋并添加到行中
         documentation += tabulate(rows, headers, tablefmt="pipe") + "\n\n"
     return documentation
 ?
 ?
 # 生成docx格式的數(shù)據(jù)庫文檔
 def generate_docx_documentation(tables_info):
     doc = Document()
     doc.add_heading('數(shù)據(jù)庫文檔', 0)
     doc.add_paragraph(f"數(shù)據(jù)庫地址:{host_entry.get()}")
     doc.add_paragraph(f"用戶名:{user_entry.get()}")
     doc.add_paragraph(f"數(shù)據(jù)庫名稱:{database_entry.get()}")
     for table_info in tables_info:
         table_name = table_info["table_name"]
         structure = table_info["structure"]
         doc.add_heading(table_name, level=1)
 ?
         # 創(chuàng)建帶邊框的表格
         table = doc.add_table(rows=1, cols=7)
         table.style = 'Table Grid'  # 設(shè)置表格樣式為帶邊框的樣式
         table.autofit = False  # 禁止自動調(diào)整列寬
 ?
         hdr_cells = table.rows[0].cells
         hdr_cells[0].text = '字段'
         hdr_cells[1].text = '類型'
         hdr_cells[2].text = '允許空值'
         hdr_cells[3].text = '鍵'
         hdr_cells[4].text = '默認值'
         hdr_cells[5].text = '額外信息'
         hdr_cells[6].text = '注釋'  # 添加注釋列
         for field_info in structure:
             row_cells = table.add_row().cells
             row_cells[0].text = field_info[0]
             row_cells[1].text = field_info[1]
             row_cells[2].text = field_info[2]
             row_cells[3].text = field_info[3]
             row_cells[
                 4].text = field_info[4] if field_info[4] is not None else ""
             row_cells[5].text = field_info[5]
             row_cells[6].text = get_field_comment(table_name,
                                                   field_info[0])  # 獲取并顯示字段注釋
     return doc
 ?
 ?
 # 創(chuàng)建標(biāo)簽和輸入框
 def create_input_fields(root, fields):
     entries = {}
     for row, (label_text, entry_text) in enumerate(fields):
         label = Label(root, text=label_text)
         label.grid(row=row, column=0, padx=10, pady=10, sticky="w")
         entry = Entry(root)
         entry.grid(row=row, column=1, padx=10, pady=10)
         entry.insert(0, entry_text)
         entries[label_text] = entry
     # 添加文檔類型選擇器
     label = Label(root, text="文檔類型:")
     label.grid(row=len(fields), column=0, padx=10, pady=10, sticky="w")
     doc_type = StringVar(root)
     doc_type.set("Markdown")  # 默認選擇 Markdown
     doc_type_menu = OptionMenu(root, doc_type, "Markdown", "Docx")
     doc_type_menu.grid(row=len(fields), column=1, padx=10, pady=10, sticky="w")
     entries["文檔類型:"] = doc_type
     return entries
 ?
 ?
 # 生成文檔
 def generate_document():
     global conn  # 在函數(shù)內(nèi)部使用全局變量 conn
     conn = connect_to_database()
     if conn:
         tables_info = get_table_info(conn)
         if entries["文檔類型:"].get() == "Markdown":  # 獲取文檔類型
             documentation = generate_markdown_documentation(tables_info)
             with open("數(shù)據(jù)庫文檔.md", "w", encoding="utf-8") as file:
                 file.write(documentation)
             messagebox.showinfo("成功", "Markdown文檔生成成功!")
         elif entries["文檔類型:"].get() == "Docx":
             doc = generate_docx_documentation(tables_info)
             doc.save("數(shù)據(jù)庫文檔.docx")
             messagebox.showinfo("成功", "Docx文檔生成成功!")
 ?
 ?
 # 創(chuàng)建主窗口
 root = Tk()
 root.title("數(shù)據(jù)庫文檔生成器")
 root.geometry("400x300")
 ?
 # 標(biāo)簽和輸入框的內(nèi)容
 fields = [("主機地址:", ""), ("用戶名:", ""), ("密碼:", ""), ("數(shù)據(jù)庫名稱:", "")]
 ?
 # 創(chuàng)建標(biāo)簽和輸入框
 entries = create_input_fields(root, fields)
 ?
 # 獲取輸入框的內(nèi)容
 host_entry = entries["主機地址:"]
 user_entry = entries["用戶名:"]
 password_entry = entries["密碼:"]
 database_entry = entries["數(shù)據(jù)庫名稱:"]
 ?
 # 生成文檔按鈕
 generate_button = Button(root, text="生成文檔", command=generate_document)
 generate_button.grid(row=len(fields) + 1, columnspan=2, padx=10, pady=10)
 ?
 root.mainloop()

以上就是使用Python實現(xiàn)數(shù)據(jù)庫文檔生成工具的詳細內(nèi)容,更多關(guān)于Python生成數(shù)據(jù)庫文檔的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python實現(xiàn)梯度下降算法的實例詳解

    python實現(xiàn)梯度下降算法的實例詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于python實現(xiàn)梯度下降算法的實例詳解內(nèi)容,需要的朋友們可以參考下。
    2020-08-08
  • python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密)

    python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密)

    這篇文章主要為大家介紹了python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Python random模塊制作簡易的四位數(shù)驗證碼

    Python random模塊制作簡易的四位數(shù)驗證碼

    這篇文章主要介紹了Python random模塊制作簡易的四位數(shù)驗證碼,文中給大家提到了python中random模塊的相關(guān)知識,需要的朋友可以參考下
    2020-02-02
  • 一百行python代碼將圖片轉(zhuǎn)成字符畫

    一百行python代碼將圖片轉(zhuǎn)成字符畫

    這篇文章主要為大家詳細介紹了一百行python代碼將圖片轉(zhuǎn)成字符畫 ,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Pycharm debug調(diào)試時帶參數(shù)過程解析

    Pycharm debug調(diào)試時帶參數(shù)過程解析

    這篇文章主要介紹了Pycharm debug調(diào)試時帶參數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • python中的格式化輸出用法總結(jié)

    python中的格式化輸出用法總結(jié)

    這篇文章主要介紹了python中的格式化輸出用法,分析了Python格式化輸出的種類并結(jié)合實例形式總結(jié)了針對浮點數(shù)的格式化輸出方法,需要的朋友可以參考下
    2016-07-07
  • Python urllib庫如何添加headers過程解析

    Python urllib庫如何添加headers過程解析

    這篇文章主要介紹了Python urllib庫如何添加headers過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • python數(shù)據(jù)類型強制轉(zhuǎn)換實例詳解

    python數(shù)據(jù)類型強制轉(zhuǎn)換實例詳解

    這篇文章主要介紹了python數(shù)據(jù)類型強制轉(zhuǎn)換實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Python2和Python3的共存和切換使用

    Python2和Python3的共存和切換使用

    這篇文章主要介紹了Python2和Python3的共存和切換使用,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • Python更新數(shù)據(jù)庫腳本兩種方法及對比介紹

    Python更新數(shù)據(jù)庫腳本兩種方法及對比介紹

    這篇文章給大家介紹了Python更新數(shù)據(jù)庫腳本兩種方法及數(shù)據(jù)庫查詢?nèi)N方式,然后在文章下面給大家介紹了兩種方式對比介紹,非常不錯,感興趣的朋友參考下吧
    2017-07-07

最新評論