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

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

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

1、功能說(shuō)明

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

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

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

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

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

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

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

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

2、設(shè)計(jì)思路

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

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

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)庫(kù)。

3、主要代碼塊

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

最后通過(guò)QThread子線程調(diào)用業(yè)務(wù)邏輯代碼塊,從而實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)以及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ù)庫(kù)導(dǎo)出為Excel)')
        self.setWindowIcon(QIcon(':/analysis.ico'))
        self.resize(300, 400)

        self.database_ip_label = QLabel()
        self.database_ip_label.setText('數(shù)據(jù)庫(kù)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ù)庫(kù)端口:')

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

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

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

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

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

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

        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('日志處理過(guò)程區(qū)域...')
        self.brower.ensureCursorVisible()

        self.start_btn = QPushButton()
        self.start_btn.setText('開(kāi)始導(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)的主要操作,供小伙伴們開(kāi)發(fā)參考。

下面是關(guān)于QThread子線程的部分創(chuàng)建過(guò)程,可以將業(yè)務(wù)相關(guān)的處理放到子線程中執(zhí)行,這樣便不會(huì)導(dǎo)致UI頁(yè)面主線程出現(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ù)庫(kù)表導(dǎo)出Excel表格小工具的文章就介紹到這了,更多相關(guān)PyQt5數(shù)據(jù)庫(kù)導(dǎo)出Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論