python3+PyQt5 數(shù)據(jù)庫(kù)編程--增刪改實(shí)例
本文通過(guò)python3+pyqt5改寫實(shí)現(xiàn)了python Qt gui 編程變成15章的excise例子。
#!/usr/bin/env python3
import os
import sys
from PyQt5.QtCore import (QFile, QVariant, Qt)
from PyQt5.QtWidgets import (QApplication, QDialog, QDialogButtonBox, QMenu,
QMessageBox, QTableView, QVBoxLayout)
from PyQt5.QtSql import (QSqlDatabase, QSqlQuery, QSqlTableModel)
MAC = True
try:
from PyQt5.QtGui import qt_mac_set_native_menubar
except ImportError:
MAC = False
ID, CATEGORY, SHORTDESC, LONGDESC = range(4)
class ReferenceDataDlg(QDialog):
def __init__(self, parent=None):
super(ReferenceDataDlg, self).__init__(parent)
self.model = QSqlTableModel(self)
self.model.setTable("reference")
self.model.setSort(ID, Qt.AscendingOrder)
self.model.setHeaderData(ID, Qt.Horizontal, "ID")
self.model.setHeaderData(CATEGORY, Qt.Horizontal,"Category")
self.model.setHeaderData(SHORTDESC, Qt.Horizontal,"Short Desc.")
self.model.setHeaderData(LONGDESC, Qt.Horizontal,"Long Desc.")
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.setSelectionMode(QTableView.SingleSelection)
self.view.setSelectionBehavior(QTableView.SelectRows)
self.view.setColumnHidden(ID, True)
self.view.resizeColumnsToContents()
buttonBox = QDialogButtonBox()
addButton = buttonBox.addButton("&Add",
QDialogButtonBox.ActionRole)
deleteButton = buttonBox.addButton("&Delete",
QDialogButtonBox.ActionRole)
sortButton = buttonBox.addButton("&Sort",
QDialogButtonBox.ActionRole)
if not MAC:
addButton.setFocusPolicy(Qt.NoFocus)
deleteButton.setFocusPolicy(Qt.NoFocus)
sortButton.setFocusPolicy(Qt.NoFocus)
menu = QMenu(self)
sortByCategoryAction = menu.addAction("Sort by &Category")
sortByDescriptionAction = menu.addAction("Sort by &Description")
sortByIDAction = menu.addAction("Sort by &ID")
sortButton.setMenu(menu)
closeButton = buttonBox.addButton(QDialogButtonBox.Close)
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addWidget(buttonBox)
self.setLayout(layout)
addButton.clicked.connect(self.addRecord)
deleteButton.clicked.connect(self.deleteRecord)
sortByCategoryAction.triggered.connect(lambda:self.sort(CATEGORY))
sortByDescriptionAction.triggered.connect(lambda:self.sort(SHORTDESC))
sortByIDAction.triggered.connect(lambda:self.sort(ID))
closeButton.clicked.connect(self.accept)
self.setWindowTitle("Reference Data")
def addRecord(self):
row = self.model.rowCount()
self.model.insertRow(row)
index = self.model.index(row, CATEGORY)
self.view.setCurrentIndex(index)
self.view.edit(index)
def deleteRecord(self):
index = self.view.currentIndex()
if not index.isValid():
return
record = self.model.record(index.row())
category = record.value(CATEGORY)
desc = record.value(SHORTDESC)
if (QMessageBox.question(self, "Reference Data",
("Delete {0} from category {1}?"
.format(desc,category)),
QMessageBox.Yes|QMessageBox.No) ==
QMessageBox.No):
return
self.model.removeRow(index.row())
self.model.submitAll()
self.model.select()
def sort(self, column):
self.model.setSort(column, Qt.AscendingOrder)
self.model.select()
def main():
app = QApplication(sys.argv)
filename = os.path.join(os.path.dirname(__file__), "reference.db")
create = not QFile.exists(filename)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(filename)
if not db.open():
QMessageBox.warning(None, "Reference Data",
"Database Error: {0}".format(db.lastError().text()))
sys.exit(1)
if create:
query = QSqlQuery()
query.exec_("""CREATE TABLE reference (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
category VARCHAR(30) NOT NULL,
shortdesc VARCHAR(20) NOT NULL,
longdesc VARCHAR(80))""")
form = ReferenceDataDlg()
form.show()
sys.exit(app.exec_())
main()
運(yùn)行結(jié)果:

以上這篇python3+PyQt5 數(shù)據(jù)庫(kù)編程--增刪改實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python+PyQt5實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)備份神器
- 基于PyQt5實(shí)現(xiàn)SqlServer數(shù)據(jù)庫(kù)表導(dǎo)出Excel表格小工具
- Python+PyQt5實(shí)現(xiàn)數(shù)據(jù)庫(kù)表格動(dòng)態(tài)增刪改
- PyQt5?python?數(shù)據(jù)庫(kù)?表格動(dòng)態(tài)增刪改詳情
- Python GUI教程之在PyQt5中使用數(shù)據(jù)庫(kù)的方法
- pyqt5數(shù)據(jù)庫(kù)使用詳細(xì)教程(打包解決方案)
- python3+PyQt5使用數(shù)據(jù)庫(kù)表視圖
- python3+PyQt5使用數(shù)據(jù)庫(kù)窗口視圖
- PyQt5與數(shù)據(jù)庫(kù)交互的項(xiàng)目實(shí)踐
相關(guān)文章
用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例
下面小編就為大家分享一篇用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想過(guò)來(lái)看看吧2017-12-12
11個(gè)Python Pandas小技巧讓你的工作更高效(附代碼實(shí)例)
這篇文章主要介紹了11個(gè)Python Pandas小技巧讓你的工作更高效(附代碼實(shí)例),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Pytorch基礎(chǔ)教程之torchserve模型部署解析
torchserve是基于netty網(wǎng)絡(luò)框架實(shí)現(xiàn)的,底層使用EpollServerSocketChannel服務(wù)進(jìn)行網(wǎng)絡(luò)通信,通過(guò)epoll多路復(fù)用技術(shù)實(shí)現(xiàn)高并發(fā)網(wǎng)絡(luò)連接處理,這篇文章主要介紹了Pytorch基礎(chǔ)教程之torchserve模型部署和推理,需要的朋友可以參考下2023-07-07
Python創(chuàng)建類的方法及成員訪問的相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)的是關(guān)于Python基礎(chǔ)的相關(guān)知識(shí),文章圍繞著Python類的方法及成員訪問展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Pandas實(shí)現(xiàn)dataframe和np.array的相互轉(zhuǎn)換
今天小編就為大家分享一篇Pandas實(shí)現(xiàn)dataframe和np.array的相互轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Python實(shí)現(xiàn)爬取逐浪小說(shuō)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)爬取逐浪小說(shuō)的方法,基于Python的正則匹配功能實(shí)現(xiàn)爬取小說(shuō)頁(yè)面標(biāo)題、鏈接及正文等功能,需要的朋友可以參考下2015-07-07
利用Hyperic調(diào)用Python實(shí)現(xiàn)進(jìn)程守護(hù)
這篇文章主要為大家詳細(xì)介紹了利用Hyperic調(diào)用Python實(shí)現(xiàn)進(jìn)程守護(hù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Python實(shí)現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中棧的操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)基本數(shù)據(jù)結(jié)構(gòu)中棧的操作,包括基于Python實(shí)現(xiàn)棧的定義、入棧、出棧、判斷??栈驐M等情況,需要的朋友可以參考下2017-12-12
一文帶你掌握Python中textwrap庫(kù)文本包裝的藝術(shù)
在Python編程中,處理文本是一項(xiàng)基礎(chǔ)且常見的任務(wù),textwrap模塊正是為此而生,它提供了一系列簡(jiǎn)單而強(qiáng)大的工具,幫助我們優(yōu)雅地完成文本包裝和格式化工作,下面就跟隨小編來(lái)看看它的具體使用吧2024-12-12

