PyQt5?python?數(shù)據(jù)庫?表格動(dòng)態(tài)增刪改詳情
(一)、手動(dòng)連接數(shù)據(jù)庫
與下一個(gè)的程序連接數(shù)據(jù)庫是獨(dú)立的2個(gè)部分
(4)屬性中的連接字符串
(二)、編程中使用數(shù)據(jù)庫
效果圖:
from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5 import QtCore from copy import copy import sys import pymysql ins="insert into student (Sno,Sname,Sage,Ssex,Stele) values ('{}','{}','{}','{}','{}');" delete="delete from student where Sno={}" sel="select * from student" upd="update student set Sname='{}',Sage='{}',Ssex='{}',Stele='{}' where Sno={};" def getCursor(): ? ? """ ? ? :return: 返回操作數(shù)據(jù)庫的cursor ? ? """ ? ? conn = pymysql.connect(host='127.0.0.1' ?# 連接名稱,默認(rèn)127.0.0.1 ? ? ? ? ? ? ? ? ? ? ? ? ? ?, user='root' ?# 用戶名 ? ? ? ? ? ? ? ? ? ? ? ? ? ?, passwd='' ?# 密碼 ? ? ? ? ? ? ? ? ? ? ? ? ? ?, port=3306 ?# 端口,默認(rèn)為3306 ? ? ? ? ? ? ? ? ? ? ? ? ? ?, db='mysql' ?# 數(shù)據(jù)庫名稱 ? ? ? ? ? ? ? ? ? ? ? ? ? ?, charset='utf8' ?# 字符編碼 ? ? ? ? ? ? ? ? ? ? ? ? ? ?) ? ? return conn def ExecuSQL(argv): ? ? """ ? ? 執(zhí)行數(shù)據(jù)庫的語句,但是沒有返回值 ? ? :param argv: ? ? """ ? ? conn=getCursor() ? ? cur = conn.cursor() ?# 生成游標(biāo)對(duì)象 ? ? cur.execute(argv) ?# 執(zhí)行SQL語句 ? ? conn.commit() ? ? cur.close() ?# 關(guān)閉游標(biāo) ? ? conn.close() ?# 關(guān)閉連接 def getData(argv): ? ? """ ? ? 執(zhí)行數(shù)據(jù)庫的語句,有返回值 ? ? :param argv: ? ? """ ? ? conn = getCursor() ? ? cur = conn.cursor() ?# 生成游標(biāo)對(duì)象 ? ? cur.execute(argv) ?# 執(zhí)行SQL語句 ? ? data = cur.fetchall() ?# 通過fetchall方法獲得數(shù)據(jù) ? ? cur.close() ?# 關(guān)閉游標(biāo) ? ? conn.close() ?# 關(guān)閉連接 ? ? return data class Example(QWidget): ? ? def __init__(self, parent=None): ? ? ? ? super(Example, self).__init__(parent) ? ? ? ? hhbox = QHBoxLayout() ?# 橫向布局 ? ? ? ? hhbox_1=QHBoxLayout() ? ? ? ? vbox=QVBoxLayout() ? ? ? ? self.displayList = [] ? ? ? ? self.saveList = [] ? ? ? ? self.table = QTableWidget() ? ? ? ? self.addItem=QPushButton("添加數(shù)據(jù)") ? ? ? ? self.searchItem=QPushButton("刷新數(shù)據(jù)") ? ? ? ? self.deleteItem = QPushButton("刪除數(shù)據(jù)") ? ? ? ? self.saveItem=QPushButton("保存數(shù)據(jù)") ? ? ? ? self.table_sitting() ? ? ? ? hhbox.addWidget(self.table) ?# 把表格加入布局 ? ? ? ? hhbox_1.addWidget(self.addItem) ? ? ? ? hhbox_1.addWidget(self.searchItem) ? ? ? ? hhbox_1.addWidget(self.deleteItem) ? ? ? ? hhbox_1.addWidget(self.saveItem) ? ? ? ? vbox.addLayout(hhbox) ? ? ? ? vbox.addLayout(hhbox_1) ? ? ? ? self.setLayout(vbox) ?# 創(chuàng)建布局 ? ? ? ? self.setWindowTitle("數(shù)據(jù)庫—表格") ? ? ? ? self.setWindowIcon(QIcon("icon.png")) ? ? ? ? self.connecter() ? ? ? ? self.resize(680, 600) ? ? ? ? self.show() ? ? def connecter(self): ? ? ? ? self.addItem.clicked.connect(self._addItem) ? ? ? ? self.deleteItem.clicked.connect(self._deleteItem) ? ? ? ? self.searchItem.clicked.connect(self._redraw) ? ? ? ? self.saveItem.clicked.connect(self._saveItem) ? ? ? ? self.table.itemChanged.connect(self._dataChanged) ? ? def _dataChanged(self): ? ? ? ? """ ? ? ? ? 一旦檢測到數(shù)據(jù)改變,則進(jìn)行檢查, ? ? ? ? 選擇添加新數(shù)據(jù)還是對(duì)原數(shù)據(jù)進(jìn)行修改 ? ? ? ? :return: ? ? ? ? """ ? ? ? ? row_select = self.table.selectedItems() ? ? ? ? if len(row_select) == 0: ? ? ? ? ? ? return ? ? ? ? row= row_select[0].row() ? ? ? ? content = (self.table.item(row, 0).text(), self.table.item(row, 1).text(), ? ? ? ? ? ? ? ? ? ?self.table.item(row, 2).text(), self.table.item(row, 3).text(), ? ? ? ? ? ? ? ? ? ?self.table.item(row, 4).text()) ? ? ? ? if row<=len(self.displayList): ? ? ? ? ? ? print("修改行",content) ? ? ? ? ? ? self.displayList[row-1]=content ? ? ? ? else: ? ? ? ? ? ? print("最新行",content) ? ? ? ? ? ? self.displayList.append(content) ? ? def _addItem(self): ? ? ? ? """ ? ? ? ? 添加空白行按鈕的觸發(fā)事件 ? ? ? ? 添加后刷新視圖 ? ? ? ? """ ? ? ? ? num = self.table.rowCount()-1 ? ? ? ? self.newLine(num) ? ? ? ? self.update() ? ? def init(self): ? ? ? ? """ ? ? ? ? 初始化操作 ? ? ? ? 即從數(shù)據(jù)庫加載數(shù)據(jù) ? ? ? ? """ ? ? ? ? argv="select * from student" ? ? ? ? data=getData(argv) ? ? ? ? print("初始化") ? ? ? ? for index,item in enumerate(data): ? ? ? ? ? ? self.newLine(index+1,item=item) ? ? ? ? ? ? self.displayList.append(item) ? ? ? ? self.saveList=copy(self.displayList) ? ? ? ? self.update() ? ? def _redraw(self): ? ? ? ? """ ? ? ? ? repaint即刷新數(shù)據(jù), ? ? ? ? 用保存的數(shù)據(jù)覆蓋未保存的數(shù)據(jù) ? ? ? ? """ ? ? ? ? self.table.setRowCount(0) ? ? ? ? self.table.clearContents() ? ? ? ? self.table_sitting(flag=0) ? ? ? ? for index,item in enumerate(self.saveList): ? ? ? ? ? ? self.newLine(index+1,item) ? ? ? ? self.update() ? ? def _deleteItem(self): ? ? ? ? """ ? ? ? ? 若有選中行,點(diǎn)擊刪除后即可刪除 ? ? ? ? :return: ? ? ? ? """ ? ? ? ? # ExecuSQL() ? ? ? ? row_select = self.table.selectedItems() ? ? ? ? if len(row_select) == 0: ? ? ? ? ? ? return ? ? ? ? id = row_select[0].row() ? ? ? ? if int(id)<len(self.displayList): ? ? ? ? ? ? print("刪除一條數(shù)據(jù)") ? ? ? ? ? ? self.displayList.pop(id-1) ? ? ? ? self.header.pop() ? ? ? ? self.table.removeRow(row_select[0].row()) ? ? ? ? self.update() ? ? def _saveItem(self): ? ? ? ? """ ? ? ? ? 點(diǎn)擊保存需要 ? ? ? ? 篩選出需要更新的數(shù)據(jù) ? ? ? ? 需要?jiǎng)h除的數(shù)據(jù) ? ? ? ? 需要添加的數(shù)據(jù) ? ? ? ? """ ? ? ? ? idList=[int(k[0]) for k in self.saveList] ? ? ? ? _idList=[int(k[0]) for k in self.displayList] ? ? ? ? print("點(diǎn)擊保存") ? ? ? ? # print(self.saveList) ? ? ? ? # print(self.displayList) ? ? ? ? for item in self.displayList: ? ? ? ? ? ? if item not in self.saveList: ? ? ? ? ? ? ? ? print("存在修改數(shù)據(jù)") ? ? ? ? ? ? ? ? if item[0] not in idList: ? ? ? ? ? ? ? ? ? ? sql=ins.format(item[0],item[1], item[2], item[3], item[4]) ? ? ? ? ? ? ? ? ? ? print(sql) ? ? ? ? ? ? ? ? ? ? ExecuSQL(sql) ? ? ? ? ? ? ? ? ? ? print("insert") ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? sql = upd.format(item[1], item[2], item[3], item[4],item[0]) ? ? ? ? ? ? ? ? ? ? print(sql) ? ? ? ? ? ? ? ? ? ? ExecuSQL(sql) ? ? ? ? ? ? ? ? ? ? print("update") ? ? ? ? for item in self.saveList: ? ? ? ? ? ? if item[0] not in _idList: ? ? ? ? ? ? ? ? sql = delete.format(item[0]) ? ? ? ? ? ? ? ? print(sql) ? ? ? ? ? ? ? ? ExecuSQL(sql) ? ? ? ? ? ? ? ? print("delete",item) ? ? ? ? self.saveList=copy(self.displayList) ? ? def newLine(self,num,item=None): ? ? ? ? """ ? ? ? ? :param num: 在對(duì)應(yīng)序號(hào)處的序號(hào)畫空白行 ? ? ? ? :param item: 輸入為對(duì)應(yīng)數(shù)據(jù) ? ? ? ? """ ? ? ? ? # num=self.table.rowCount() ? ? ? ? self.table.insertRow(num) ? ? ? ? _0= QTableWidgetItem("") ? ? ? ? _0.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) ? ? ? ? _1 = QTableWidgetItem("") ? ? ? ? _2 = QTableWidgetItem("") ? ? ? ? _3 = QTableWidgetItem("") ? ? ? ? _4 = QTableWidgetItem("") ? ? ? ? # item=studentInfo() ? ? ? ? if item !=None: ? ? ? ? ? ? _0.setText(str(item[0])) ? ? ? ? ? ? _1.setText(str(item[1])) ? ? ? ? ? ? _2.setText(str(item[2])) ? ? ? ? ? ? _3.setText(str(item[3])) ? ? ? ? ? ? _4.setText(str(item[4])) ? ? ? ? else: ? ? ? ? ? ? _0.setText(str(num)) ? ? ? ? self.table.setItem(num, 0, _0) ? ? ? ? self.table.setItem(num, 1, _1) ? ? ? ? self.table.setItem(num, 2, _2) ? ? ? ? self.table.setItem(num, 3, _3) ? ? ? ? self.table.setItem(num, 4, _4) ? ? ? ? self.header.append(str(num)) ? ? ? ? self.table.setVerticalHeaderLabels(self.header) ? ? ? ? self.update() ? ? def table_sitting(self,flag=1): ? ? ? ? """ ? ? ? ? :param flag: 初始化表頭和行列數(shù) ? ? ? ? """ ? ? ? ? self.header = [""] ? ? ? ? self.table.setColumnCount(5) ? ? ? ? self.table.setRowCount(2) ?# 設(shè)置表格有兩行五列 ? ? ? ? self.table.setItem(0, 0, QTableWidgetItem(" ? ? ? ? ? 學(xué)號(hào)")) ? ? ? ? self.table.setItem(0, 1, QTableWidgetItem("名字")) ? ? ? ? self.table.setItem(0, 2, QTableWidgetItem("出生日期")) ? ? ? ? self.table.setItem(0, 3, QTableWidgetItem("性別")) ? ? ? ? self.table.setItem(0, 4, QTableWidgetItem("電話號(hào)碼")) ? ? ? ? if flag: ? ? ? ? ? ? self.init() if __name__ == "__main__": ? ? app = QApplication(sys.argv) ? ? dlg = Example() ? ? sys.exit(app.exec_()) ++
到此這篇關(guān)于PyQt5 python 數(shù)據(jù)庫 表格動(dòng)態(tài)增刪改詳情的文章就介紹到這了,更多相關(guān)PyQt5 python 數(shù)據(jù)庫 表格動(dòng)態(tài)增刪改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python安裝Numpy和matplotlib的方法(推薦)
下面小編就為大家?guī)硪黄狿ython安裝Numpy和matplotlib的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Python實(shí)現(xiàn)字符串反轉(zhuǎn)的9種方法(最全)
本文主要介紹了Python實(shí)現(xiàn)字符串反轉(zhuǎn)的9種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07python結(jié)合opencv實(shí)現(xiàn)人臉檢測與跟蹤
在Python下用起來OpenCV很爽,代碼很簡潔,很清晰易懂。使用的是Haar特征的分類器,訓(xùn)練之后得到的數(shù)據(jù)存在一個(gè)xml中。下面我們就來詳細(xì)談?wù)劇?/div> 2015-06-06windows下安裝Python虛擬環(huán)境virtualenvwrapper-win
這篇文章主要介紹了windows下安裝Python虛擬環(huán)境virtualenvwrapper-win,內(nèi)容超簡單,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06基于keras 模型、結(jié)構(gòu)、權(quán)重保存的實(shí)現(xiàn)
今天小編就為大家分享一篇基于keras 模型、結(jié)構(gòu)、權(quán)重保存的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python爬蟲包BeautifulSoup學(xué)習(xí)實(shí)例(五)
這篇文章主要為大家詳細(xì)介紹了Python爬蟲包BeautifulSoup的學(xué)習(xí)實(shí)例,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2018-06-06Python中dumps與dump及l(fā)oads與load的區(qū)別
這篇文章主要介紹了Python中dumps與dump、loads與load的區(qū)別,json模塊提供了一種很簡單的方式來編碼和解碼JSON數(shù)據(jù)。其中兩個(gè)主要的函數(shù)是json.dumps()和json.loads(),需要的朋友可以參考下2022-04-04最新評(píng)論