欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于PyQt5實(shí)現(xiàn)SqlServer數(shù)據(jù)庫表導(dǎo)出Excel表格小工具

 更新時間:2023年12月03日 09:41:53   作者:Python 集中營  
這篇文章主要為大家詳細(xì)介紹了PyQt5的應(yīng)用案例之實(shí)現(xiàn)SqlServer數(shù)據(jù)庫表導(dǎo)出Excel表格小工具,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下

1、功能說明

windows桌面應(yīng)用,通過在應(yīng)用界面輸入SqlServer數(shù)據(jù)庫相關(guān)信息后一件導(dǎo)出excel表格數(shù)據(jù)。

應(yīng)用界面輸入信息如下:

數(shù)據(jù)庫IP:數(shù)據(jù)庫所在服務(wù)器的ip地址;

數(shù)據(jù)庫端口:數(shù)據(jù)庫服務(wù)的port端口;

數(shù)據(jù)庫名稱:需要連接的數(shù)據(jù)庫的名稱;

用戶名稱:需要連接的數(shù)據(jù)庫的用戶名稱;

密碼:需要連接的數(shù)據(jù)庫的密碼;

表名:需要導(dǎo)出的數(shù)據(jù)庫某張表的表名稱;

2、設(shè)計思路

界面應(yīng)用的UI設(shè)計通過python的PyQt5模塊開發(fā)窗口頁面功能,包括頁面布局槽函數(shù)關(guān)聯(lián)操作等。

數(shù)據(jù)庫連接以及數(shù)據(jù)操作使用的是python的三方非標(biāo)準(zhǔn)庫pymssql來完成數(shù)據(jù)庫層面的處理。

Excel表格數(shù)據(jù)處理使用的是常用的pandas模塊,可以快速的完成數(shù)據(jù)導(dǎo)出。

該工具使用的python模塊信息如下:

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import *

import sys

from datetime import datetime

import pymssql

import pandas as pd

import image

其中image模塊為打包后的圖片資源模塊,其他模塊均為python的標(biāo)準(zhǔn)或者非標(biāo)準(zhǔn)庫。

3、主要代碼塊

其中主要代碼塊為PyQt5應(yīng)用的UI界面以及槽函數(shù)的關(guān)聯(lián)和子線程模塊的調(diào)用等。

最后通過QThread子線程調(diào)用業(yè)務(wù)邏輯代碼塊,從而實(shí)現(xiàn)對數(shù)據(jù)庫以及Excel表格的處理。

主要代碼塊如下:

class DataBaseToExcelUI(QWidget):
    def __init__(self):
        super(DataBaseToExcelUI, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('數(shù)據(jù)導(dǎo)出(SQLSERVER數(shù)據(jù)庫導(dǎo)出為Excel)')
        self.setWindowIcon(QIcon(':/analysis.ico'))
        self.resize(300, 400)

        self.database_ip_label = QLabel()
        self.database_ip_label.setText('數(shù)據(jù)庫IP:')

        self.database_ip_in = QLineEdit()
        self.database_ip_in.setText('192.168.10.10')

        self.database_port_label = QLabel()
        self.database_port_label.setText('數(shù)據(jù)庫端口:')

        self.database_port_in = QLineEdit()
        self.database_port_in.setText('1513')

        self.database_name_label = QLabel()
        self.database_name_label.setText('數(shù)據(jù)庫名稱:')

        self.database_name_in = QLineEdit()
        self.database_name_in.setText('source_data')

        self.database_user_label = QLabel()
        self.database_user_label.setText('數(shù)據(jù)庫用戶名:')

        self.database_user_in = QLineEdit()
        self.database_user_in.setText('sa')

        self.database_pwd_label = QLabel()
        self.database_pwd_label.setText('數(shù)據(jù)庫密碼:')

        self.database_pwd_in = QLineEdit()
        self.database_pwd_in.setText('')

        self.database_table_label = QLabel()
        self.database_table_label.setText('數(shù)據(jù)表名稱:')

        self.database_table_in = QLineEdit()
        self.database_table_in.setText('table_name')

        self.brower = QTextBrowser()
        self.brower.setReadOnly(True)
        self.brower.setFont(QFont('宋體', 8))
        self.brower.setPlaceholderText('日志處理過程區(qū)域...')
        self.brower.ensureCursorVisible()

        self.start_btn = QPushButton()
        self.start_btn.setText('開始導(dǎo)出')
        self.start_btn.clicked.connect(self.start_btn_clk)

        f_box = QFormLayout()
        f_box.addRow(self.database_ip_label, self.database_ip_in)
        f_box.addRow(self.database_port_label, self.database_port_in)
        f_box.addRow(self.database_name_label, self.database_name_in)
        f_box.addRow(self.database_user_label, self.database_user_in)
        f_box.addRow(self.database_pwd_label, self.database_pwd_in)
        f_box.addRow(self.database_table_label, self.database_table_in)
        f_box.addRow(self.start_btn)
        f_box.addRow(self.brower)

        self.thread_ = WorkThread(self)
        self.thread_.message.connect(self.show_message)
        self.thread_.finished.connect(self.finished)

        self.setLayout(f_box)

    def show_message(self, text):
        cursor = self.brower.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.brower.append(text)
        self.brower.setTextCursor(cursor)
        self.brower.ensureCursorVisible()

    def finished(self, text):
        if text is True:
            self.start_btn.setEnabled(True)

    def start_btn_clk(self):
        self.start_btn.setEnabled(False)
        self.thread_.start()

以上代碼塊是應(yīng)用窗體相關(guān)的主要操作,供小伙伴們開發(fā)參考。

下面是關(guān)于QThread子線程的部分創(chuàng)建過程,可以將業(yè)務(wù)相關(guān)的處理放到子線程中執(zhí)行,這樣便不會導(dǎo)致UI頁面主線程出現(xiàn)阻塞等情況。

class WorkThread(QThread):
    message = pyqtSignal(str)
    finished = pyqtSignal(bool)

    def __init__(self, parent=None):
        super(WorkThread, self).__init__(parent)
        self.parent = parent
        self.working = True

    def __del__(self):
        self.working = False

到此這篇關(guān)于基于PyQt5實(shí)現(xiàn)SqlServer數(shù)據(jù)庫表導(dǎo)出Excel表格小工具的文章就介紹到這了,更多相關(guān)PyQt5數(shù)據(jù)庫導(dǎo)出Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論