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

基于PyQt5實現SqlServer數據庫表導出Excel表格小工具

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

1、功能說明

windows桌面應用,通過在應用界面輸入SqlServer數據庫相關信息后一件導出excel表格數據。

應用界面輸入信息如下:

數據庫IP:數據庫所在服務器的ip地址;

數據庫端口:數據庫服務的port端口;

數據庫名稱:需要連接的數據庫的名稱;

用戶名稱:需要連接的數據庫的用戶名稱;

密碼:需要連接的數據庫的密碼;

表名:需要導出的數據庫某張表的表名稱;

2、設計思路

界面應用的UI設計通過python的PyQt5模塊開發(fā)窗口頁面功能,包括頁面布局槽函數關聯操作等。

數據庫連接以及數據操作使用的是python的三方非標準庫pymssql來完成數據庫層面的處理。

Excel表格數據處理使用的是常用的pandas模塊,可以快速的完成數據導出。

該工具使用的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的標準或者非標準庫。

3、主要代碼塊

其中主要代碼塊為PyQt5應用的UI界面以及槽函數的關聯和子線程模塊的調用等。

最后通過QThread子線程調用業(yè)務邏輯代碼塊,從而實現對數據庫以及Excel表格的處理。

主要代碼塊如下:

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

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

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

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

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

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

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

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

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

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

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

        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('開始導出')
        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()

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

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

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

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

相關文章

最新評論