基于PyQt5實(shí)現(xiàn)SqlServer數(shù)據(jù)庫表導(dǎo)出Excel表格小工具
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)文章
Python數(shù)據(jù)分析Pandas?Dataframe排序操作
這篇文章主要介紹了Python數(shù)據(jù)分析Pandas?Dataframe排序操作,數(shù)據(jù)的排序是比較常用的操作,DataFrame?的排序分為兩種,一種是對索引進(jìn)行排序,另一種是對值進(jìn)行排序,接下來就分別都介紹一下,需要的小伙伴可以參考一下2022-05-05在Python中pandas.DataFrame重置索引名稱的實(shí)例
今天小編就為大家分享一篇在Python中pandas.DataFrame重置索引名稱的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Django利用Cookie實(shí)現(xiàn)反爬蟲的例子
這篇文章主要介紹了Django利用Cookie實(shí)現(xiàn)反爬蟲,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Python?functools凍結(jié)參數(shù)小技巧實(shí)現(xiàn)代碼簡潔優(yōu)化
這篇文章主要為大家介紹了Python?functools凍結(jié)參數(shù)小技巧實(shí)現(xiàn)代碼簡潔優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Python實(shí)現(xiàn)合并同一個文件夾下所有txt文件的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)合并同一個文件夾下所有txt文件的方法,涉及Python針對文件的遍歷、讀取、寫入等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04