鱼C论坛

 找回密码
 立即注册
查看: 4221|回复: 5

pyqt5从QsqlQueryModel转为QSqlTableModel报错

[复制链接]
发表于 2018-6-5 13:50:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtCore import Qt
  6. import qdarkstyle
  7. from PyQt5.QtSql import *


  8. class BookStorageViewer(QWidget):
  9.     def __init__(self):
  10.         super(BookStorageViewer, self).__init__()
  11.         self.resize(700, 500)
  12.         self.setWindowTitle("欢迎使用图书馆管理系统")
  13.         # 查询模型
  14.         self.queryModel = None
  15.         # 数据表
  16.         self.tableView = None
  17.         # 当前页
  18.         self.currentPage = 0
  19.         # 总页数
  20.         self.totalPage = 0
  21.         # 总记录数
  22.         self.totalRecord = 0
  23.         # 每页数据数
  24.         self.pageRecord = 99
  25.         self.setUpUI()

  26.     def setUpUI(self):
  27.         self.layout = QVBoxLayout()
  28.         self.Hlayout1 = QHBoxLayout()
  29.         self.Hlayout2 = QHBoxLayout()

  30.         # Hlayout1控件的初始化
  31.         self.searchEdit = QLineEdit()
  32.         self.searchEdit.setFixedHeight(32)
  33.         font = QFont()
  34.         font.setPixelSize(15)
  35.         self.searchEdit.setFont(font)

  36.         self.searchButton = QPushButton("查询")
  37.         self.searchButton.setFixedHeight(32)
  38.         self.searchButton.setFont(font)
  39.         self.searchButton.setIcon(QIcon(QPixmap("./images/search.png")))

  40.         self.condisionComboBox = QComboBox()
  41.         searchCondision = ['按书名查询', '按书号查询', '按作者查询', '按分类查询', '按出版社查询']
  42.         self.condisionComboBox.setFixedHeight(32)
  43.         self.condisionComboBox.setFont(font)
  44.         self.condisionComboBox.addItems(searchCondision)

  45.         self.Hlayout1.addWidget(self.searchEdit)
  46.         self.Hlayout1.addWidget(self.searchButton)
  47.         self.Hlayout1.addWidget(self.condisionComboBox)

  48.         # Hlayout2初始化
  49.         self.jumpToLabel = QLabel("跳转到第")
  50.         self.pageEdit = QLineEdit()
  51.         self.pageEdit.setFixedWidth(30)
  52.         s = "/" + str(self.totalPage) + "页"
  53.         self.pageLabel = QLabel(s)
  54.         self.jumpToButton = QPushButton("跳转")
  55.         self.prevButton = QPushButton("前一页")
  56.         self.prevButton.setFixedWidth(60)
  57.         self.backButton = QPushButton("后一页")
  58.         self.backButton.setFixedWidth(60)

  59.         Hlayout = QHBoxLayout()
  60.         Hlayout.addWidget(self.jumpToLabel)
  61.         Hlayout.addWidget(self.pageEdit)
  62.         Hlayout.addWidget(self.pageLabel)
  63.         Hlayout.addWidget(self.jumpToButton)
  64.         Hlayout.addWidget(self.prevButton)
  65.         Hlayout.addWidget(self.backButton)
  66.         widget = QWidget()
  67.         widget.setLayout(Hlayout)
  68.         widget.setFixedWidth(300)
  69.         self.Hlayout2.addWidget(widget)

  70.         # tableView
  71.         # 序号,书名,书号,作者,分类,出版社,出版时间,库存,剩余可借
  72.         self.db = QSqlDatabase.addDatabase("QSQLITE")
  73.         self.db.setDatabaseName('./db/LibraryManagement.db')
  74.         self.db.open()
  75.         self.tableView = QTableView()
  76.         self.tableView.horizontalHeader().setStretchLastSection(True)
  77.         self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
  78.         self.tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
  79.         self.queryModel = QSqlTableModel()
  80.         self.searchButtonClicked()
  81.         self.tableView.setModel(self.queryModel)

  82.         self.queryModel.setHeaderData(0, Qt.Horizontal, "书名")
  83.         self.queryModel.setHeaderData(1, Qt.Horizontal, "书号")
  84.         self.queryModel.setHeaderData(2, Qt.Horizontal, "作者")
  85.         self.queryModel.setHeaderData(3, Qt.Horizontal, "分类")
  86.         self.queryModel.setHeaderData(4, Qt.Horizontal, "出版社")
  87.         self.queryModel.setHeaderData(5, Qt.Horizontal, "出版时间")
  88.         self.queryModel.setHeaderData(6, Qt.Horizontal, "库存")
  89.         self.queryModel.setHeaderData(7, Qt.Horizontal, "剩余可借")
  90.         self.queryModel.setHeaderData(8, Qt.Horizontal, "总借阅次数")
  91.         #self.queryModel.select()

  92.         self.layout.addLayout(self.Hlayout1)
  93.         self.layout.addWidget(self.tableView)
  94.         self.layout.addLayout(self.Hlayout2)
  95.         self.setLayout(self.layout)
  96.         self.searchButton.clicked.connect(self.searchButtonClicked)
  97.         self.prevButton.clicked.connect(self.prevButtonClicked)
  98.         self.backButton.clicked.connect(self.backButtonClicked)
  99.         self.jumpToButton.clicked.connect(self.jumpToButtonClicked)
  100.         self.searchEdit.returnPressed.connect(self.searchButtonClicked)

  101.     def setButtonStatus(self):
  102.         if(self.currentPage==self.totalPage):
  103.             self.prevButton.setEnabled(True)
  104.             self.backButton.setEnabled(False)
  105.         if(self.currentPage==1):
  106.             self.backButton.setEnabled(True)
  107.             self.prevButton.setEnabled(False)
  108.         if(self.currentPage<self.totalPage and self.currentPage>1):
  109.             self.prevButton.setEnabled(True)
  110.             self.backButton.setEnabled(True)

  111.     # 得到记录数
  112.     def getTotalRecordCount(self):
  113.         self.queryModel.setQuery("SELECT * FROM Book")
  114.         self.totalRecord = self.queryModel.rowCount()
  115.         return

  116.     # 得到总页数
  117.     def getPageCount(self):
  118.         self.getTotalRecordCount()
  119.         # 上取整
  120.         self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
  121.         return

  122.     # 分页记录查询
  123.     def recordQuery(self, index):
  124.         #queryCondition = ""
  125.         conditionChoice = self.condisionComboBox.currentText()
  126.         if (conditionChoice == "按书名查询"):
  127.             conditionChoice = 'BookName'
  128.         elif (conditionChoice == "按书号查询"):
  129.             conditionChoice = 'BookId'
  130.         elif (conditionChoice == "按作者查询"):
  131.             conditionChoice = 'Auth'
  132.         elif (conditionChoice == '按分类查询'):
  133.             conditionChoice = 'Category'
  134.         else:
  135.             conditionChoice = 'Publisher'

  136.         if (self.searchEdit.text() == ""):
  137.             queryCondition = "select * from Book"
  138.             self.queryModel.setQuery(queryCondition)
  139.             self.totalRecord = self.queryModel.rowCount()
  140.             self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
  141.             label = "/" + str(int(self.totalPage)) + "页"
  142.             self.pageLabel.setText(label)
  143.             queryCondition = ("select * from Book ORDER BY %s  limit %d,%d " % (conditionChoice,index, self.pageRecord))
  144.             self.queryModel.setQuery(queryCondition)
  145.             self.setButtonStatus()
  146.             return

  147.         # 得到模糊查询条件
  148.         temp = self.searchEdit.text()
  149.         s = '%'
  150.         for i in range(0, len(temp)):
  151.             s = s + temp[i] + "%"
  152.         queryCondition = ("SELECT * FROM Book WHERE %s LIKE '%s' ORDER BY %s " % (
  153.             conditionChoice, s,conditionChoice))
  154.         self.queryModel.setQuery(queryCondition)
  155.         self.totalRecord = self.queryModel.rowCount()
  156.         # 当查询无记录时的操作
  157.         if(self.totalRecord==0):
  158.             print(QMessageBox.information(self,"提醒","查询无记录",QMessageBox.Yes,QMessageBox.Yes))
  159.             queryCondition = "select * from Book"
  160.             self.queryModel.setQuery(queryCondition)
  161.             self.totalRecord = self.queryModel.rowCount()
  162.             self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
  163.             label = "/" + str(int(self.totalPage)) + "页"
  164.             self.pageLabel.setText(label)
  165.             queryCondition = ("select * from Book ORDER BY %s  limit %d,%d " % (conditionChoice,index, self.pageRecord))
  166.             self.queryModel.setQuery(queryCondition)
  167.             self.setButtonStatus()
  168.             return
  169.         self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
  170.         label = "/" + str(int(self.totalPage)) + "页"
  171.         self.pageLabel.setText(label)
  172.         queryCondition = ("SELECT * FROM Book WHERE %s LIKE '%s' ORDER BY %s LIMIT %d,%d " % (
  173.             conditionChoice, s, conditionChoice,index, self.pageRecord))
  174.         self.queryModel.setQuery(queryCondition)
  175.         self.setButtonStatus()
  176.         return

  177.     # 点击查询
  178.     def searchButtonClicked(self):
  179.         self.currentPage = 1
  180.         self.pageEdit.setText(str(self.currentPage))
  181.         #self.getPageCount()
  182.         s = "/" + str(int(self.totalPage)) + "页"
  183.         self.pageLabel.setText(s)
  184.         index = (self.currentPage - 1) * self.pageRecord
  185.         self.recordQuery(index)
  186.         return

  187.     # 向前翻页
  188.     def prevButtonClicked(self):
  189.         self.currentPage -= 1
  190.         if (self.currentPage <= 1):
  191.             self.currentPage = 1
  192.         self.pageEdit.setText(str(self.currentPage))
  193.         index = (self.currentPage - 1) * self.pageRecord
  194.         self.recordQuery(index)
  195.         return

  196.     # 向后翻页
  197.     def backButtonClicked(self):
  198.         self.currentPage += 1
  199.         if (self.currentPage >= int(self.totalPage)):
  200.             self.currentPage = int(self.totalPage)
  201.         self.pageEdit.setText(str(self.currentPage))
  202.         index = (self.currentPage - 1) * self.pageRecord
  203.         self.recordQuery(index)
  204.         return

  205.     # 点击跳转
  206.     def jumpToButtonClicked(self):
  207.         if (self.pageEdit.text().isdigit()):
  208.             self.currentPage = int(self.pageEdit.text())
  209.             if (self.currentPage > self.totalPage):
  210.                 self.currentPage = self.totalPage
  211.             if (self.currentPage <= 1):
  212.                 self.currentPage = 1
  213.         else:
  214.             self.currentPage = 1
  215.         index = (self.currentPage - 1) * self.pageRecord
  216.         self.pageEdit.setText(str(self.currentPage))
  217.         self.recordQuery(index)
  218.         return


  219. if __name__ == "__main__":
  220.     app = QApplication(sys.argv)
  221.     app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
  222.     app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
  223.     mainMindow = BookStorageViewer()
  224.     mainMindow.show()
  225.     sys.exit(app.exec_())
复制代码
报错信息TypeError: setQuery(self, QSqlQuery): argument 1 has unexpected type 'str'   155行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-6-5 14:10:39 | 显示全部楼层
self.queryModel.setQuery(queryCondition)
这句总是报错,求怎么改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-7 15:48:21 From FishC Mobile | 显示全部楼层
顶帖
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-6-8 14:41:27 | 显示全部楼层
不报错了,改成setTable就好了,但显示不了数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-7 15:26:38 | 显示全部楼层
楼主后来tableview成功显示了吗?同样报错那句,改成setTable还要有其他操作吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-7 15:28:30 | 显示全部楼层
希望交流一下,我也做到数据库连接显示这步,此处要queryModel何不直接设置tableModel
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-30 16:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表