欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3+PyQt5 數(shù)據(jù)庫編程--增刪改實(shí)例

 更新時(shí)間:2019年06月17日 11:32:49   作者:basisworker  
今天小編就為大家分享一篇python3+PyQt5 數(shù)據(jù)庫編程--增刪改實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

本文通過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ù)庫編程--增刪改實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用django-suit為django 1.7 admin后臺(tái)添加模板

    使用django-suit為django 1.7 admin后臺(tái)添加模板

    前面我們介紹了Django-grappelli給admin添加模板,可是使用中發(fā)現(xiàn)inline有點(diǎn)問題,所以就換了今天我們要談的Django-suit,貌似要稍微好一些
    2014-11-11
  • 淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn)

    淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn)

    這篇文章主要介紹了淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 一文搞懂python 中的迭代器和生成器

    一文搞懂python 中的迭代器和生成器

    這篇文章主要介紹了python 中的迭代器和生成器簡(jiǎn)單介紹,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 詳細(xì)介紹Python中的set集合

    詳細(xì)介紹Python中的set集合

    本文詳細(xì)介紹了Python中set集合的基本概念和詳細(xì)用法,希望對(duì)讀者朋友們有所幫助。需要的朋友可以參考下面具體的文章內(nèi)容
    2021-09-09
  • Python實(shí)現(xiàn)針對(duì)含中文字符串的截取功能示例

    Python實(shí)現(xiàn)針對(duì)含中文字符串的截取功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)針對(duì)含中文字符串的截取功能,結(jié)合具體實(shí)例形式分析了Python針對(duì)utf-8及gb18030編碼的中文字符串截取操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • 使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL的方法

    使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL的方法

    在日常工作中,我們經(jīng)常會(huì)遇到需要處理大量文件并將數(shù)據(jù)存儲(chǔ)至數(shù)據(jù)庫或整合到一個(gè)文件的需求,本文將向大家展示如何使用Python處理Excel文件并將數(shù)據(jù)存儲(chǔ)到PostgreSQL數(shù)據(jù)庫中,需要的朋友可以參考下
    2024-01-01
  • python使用beautifulsoup4爬取酷狗音樂代碼實(shí)例

    python使用beautifulsoup4爬取酷狗音樂代碼實(shí)例

    這篇文章主要介紹了python使用beautifulsoup4爬取酷狗音樂代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 提升Python編程效率的列表操作方法示例

    提升Python編程效率的列表操作方法示例

    這篇文章主要為大家介紹了提升Python編程效率的列表操作方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • python?Pydub簡(jiǎn)單易用的音頻處理庫使用實(shí)例探索

    python?Pydub簡(jiǎn)單易用的音頻處理庫使用實(shí)例探索

    Pydub是一個(gè)簡(jiǎn)單易用的Python庫,它讓音頻處理變得像處理列表或字符串一樣簡(jiǎn)單,你可以用Pydub來剪輯、合并、調(diào)整音頻文件,以及執(zhí)行許多其他的音頻處理任務(wù),它支持多種音頻格式,包括常見的MP3、WAV和AAC
    2024-01-01
  • Python中的tuple元組詳細(xì)介紹

    Python中的tuple元組詳細(xì)介紹

    這篇文章主要介紹了Python中的tuple元組詳細(xì)介紹,本文講解了Tuple 與 list 的相同之處、Tuple 不存在的方法、用 Tuple 的好處、Tuple 與 list 的轉(zhuǎn)換等內(nèi)容,需要的朋友可以參考下
    2015-02-02

最新評(píng)論