欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python3+PyQt5 数据库编程--增删改实例

程序员文章站 2023-12-17 08:48:10
本文通过python3+pyqt5改写实现了python qt gui 编程变成15章的excise例子。 #!/usr/bin/env python3 imp...

本文通过python3+pyqt5改写实现了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()

运行结果:

python3+PyQt5 数据库编程--增删改实例

以上这篇python3+pyqt5 数据库编程--增删改实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: