0%

PyQt5搭建图书馆管理系统(6)——书籍信息查询实现

分析#

从数据库的设计可以看到,在这里我们只需要使用Book表的数据,就可以实现书籍信息的查询了

在这个功能的实现上,可以说花了我比较大的心思了,主要实现了这些功能:

  • 动态刷新,也就是说当管理员删除或者添加书籍,用户借阅归还书籍的时候动态刷新这个TableView
  • 模糊匹配
  • 按书名,作者,书号,出版社查询
  • 跳转页,翻页

代码实现#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import qdarkstyle
from PyQt5.QtSql import *


class BookStorageViewer(QWidget):
def __init__(self):
super(BookStorageViewer, self).__init__()
self.resize(700, 500)
self.setWindowTitle("欢迎使用图书馆管理系统")
# 查询模型
self.queryModel = None
# 数据表
self.tableView = None
# 当前页
self.currentPage = 0
# 总页数
self.totalPage = 0
# 总记录数
self.totalRecord = 0
# 每页数据数
self.pageRecord = 10
self.setUpUI()

def setUpUI(self):
self.layout = QVBoxLayout()
self.Hlayout1 = QHBoxLayout()
self.Hlayout2 = QHBoxLayout()

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

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

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

self.Hlayout1.addWidget(self.searchEdit)
self.Hlayout1.addWidget(self.searchButton)
self.Hlayout1.addWidget(self.condisionComboBox)

# Hlayout2初始化
self.jumpToLabel = QLabel("跳转到第")
self.pageEdit = QLineEdit()
self.pageEdit.setFixedWidth(30)
s = "/" + str(self.totalPage) + "页"
self.pageLabel = QLabel(s)
self.jumpToButton = QPushButton("跳转")
self.prevButton = QPushButton("前一页")
self.prevButton.setFixedWidth(60)
self.backButton = QPushButton("后一页")
self.backButton.setFixedWidth(60)

Hlayout = QHBoxLayout()
Hlayout.addWidget(self.jumpToLabel)
Hlayout.addWidget(self.pageEdit)
Hlayout.addWidget(self.pageLabel)
Hlayout.addWidget(self.jumpToButton)
Hlayout.addWidget(self.prevButton)
Hlayout.addWidget(self.backButton)
widget = QWidget()
widget.setLayout(Hlayout)
widget.setFixedWidth(300)
self.Hlayout2.addWidget(widget)

# tableView
# 序号,书名,书号,作者,分类,出版社,出版时间,库存,剩余可借
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName('./db/LibraryManagement.db')
self.db.open()
self.tableView = QTableView()
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.queryModel = QSqlQueryModel()
self.searchButtonClicked()
self.tableView.setModel(self.queryModel)

self.queryModel.setHeaderData(0, Qt.Horizontal, "书名")
self.queryModel.setHeaderData(1, Qt.Horizontal, "书号")
self.queryModel.setHeaderData(2, Qt.Horizontal, "作者")
self.queryModel.setHeaderData(3, Qt.Horizontal, "分类")
self.queryModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.queryModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.queryModel.setHeaderData(6, Qt.Horizontal, "库存")
self.queryModel.setHeaderData(7, Qt.Horizontal, "剩余可借")
self.queryModel.setHeaderData(8, Qt.Horizontal, "总借阅次数")

self.layout.addLayout(self.Hlayout1)
self.layout.addWidget(self.tableView)
self.layout.addLayout(self.Hlayout2)
self.setLayout(self.layout)
self.searchButton.clicked.connect(self.searchButtonClicked)
self.prevButton.clicked.connect(self.prevButtonClicked)
self.backButton.clicked.connect(self.backButtonClicked)
self.jumpToButton.clicked.connect(self.jumpToButtonClicked)
self.searchEdit.returnPressed.connect(self.searchButtonClicked)

def setButtonStatus(self):
if(self.currentPage==self.totalPage):
self.prevButton.setEnabled(True)
self.backButton.setEnabled(False)
if(self.currentPage==1):
self.backButton.setEnabled(True)
self.prevButton.setEnabled(False)
if(self.currentPage<self.totalPage and self.currentPage>1):
self.prevButton.setEnabled(True)
self.backButton.setEnabled(True)

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

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

# 分页记录查询
def recordQuery(self, index):
queryCondition = ""
conditionChoice = self.condisionComboBox.currentText()
if (conditionChoice == "按书名查询"):
conditionChoice = 'BookName'
elif (conditionChoice == "按书号查询"):
conditionChoice = 'BookId'
elif (conditionChoice == "按作者查询"):
conditionChoice = 'Auth'
elif (conditionChoice == '按分类查询'):
conditionChoice = 'Category'
else:
conditionChoice = 'Publisher'

if (self.searchEdit.text() == ""):
queryCondition = "select * from Book"
self.queryModel.setQuery(queryCondition)
self.totalRecord = self.queryModel.rowCount()
self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
label = "/" + str(int(self.totalPage)) + "页"
self.pageLabel.setText(label)
queryCondition = ("select * from Book ORDER BY %s limit %d,%d " % (conditionChoice,index, self.pageRecord))
self.queryModel.setQuery(queryCondition)
self.setButtonStatus()
return

# 得到模糊查询条件
temp = self.searchEdit.text()
s = '%'
for i in range(0, len(temp)):
s = s + temp[i] + "%"
queryCondition = ("SELECT * FROM Book WHERE %s LIKE '%s' ORDER BY %s " % (
conditionChoice, s,conditionChoice))
self.queryModel.setQuery(queryCondition)
self.totalRecord = self.queryModel.rowCount()
# 当查询无记录时的操作
if(self.totalRecord==0):
print(QMessageBox.information(self,"提醒","查询无记录",QMessageBox.Yes,QMessageBox.Yes))
queryCondition = "select * from Book"
self.queryModel.setQuery(queryCondition)
self.totalRecord = self.queryModel.rowCount()
self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
label = "/" + str(int(self.totalPage)) + "页"
self.pageLabel.setText(label)
queryCondition = ("select * from Book ORDER BY %s limit %d,%d " % (conditionChoice,index, self.pageRecord))
self.queryModel.setQuery(queryCondition)
self.setButtonStatus()
return
self.totalPage = int((self.totalRecord + self.pageRecord - 1) / self.pageRecord)
label = "/" + str(int(self.totalPage)) + "页"
self.pageLabel.setText(label)
queryCondition = ("SELECT * FROM Book WHERE %s LIKE '%s' ORDER BY %s LIMIT %d,%d " % (
conditionChoice, s, conditionChoice,index, self.pageRecord))
self.queryModel.setQuery(queryCondition)
self.setButtonStatus()
return

# 点击查询
def searchButtonClicked(self):
self.currentPage = 1
self.pageEdit.setText(str(self.currentPage))
self.getPageCount()
s = "/" + str(int(self.totalPage)) + "页"
self.pageLabel.setText(s)
index = (self.currentPage - 1) * self.pageRecord
self.recordQuery(index)
return

# 向前翻页
def prevButtonClicked(self):
self.currentPage -= 1
if (self.currentPage <= 1):
self.currentPage = 1
self.pageEdit.setText(str(self.currentPage))
index = (self.currentPage - 1) * self.pageRecord
self.recordQuery(index)
return

# 向后翻页
def backButtonClicked(self):
self.currentPage += 1
if (self.currentPage >= int(self.totalPage)):
self.currentPage = int(self.totalPage)
self.pageEdit.setText(str(self.currentPage))
index = (self.currentPage - 1) * self.pageRecord
self.recordQuery(index)
return

# 点击跳转
def jumpToButtonClicked(self):
if (self.pageEdit.text().isdigit()):
self.currentPage = int(self.pageEdit.text())
if (self.currentPage > self.totalPage):
self.currentPage = self.totalPage
if (self.currentPage <= 1):
self.currentPage = 1
else:
self.currentPage = 1
index = (self.currentPage - 1) * self.pageRecord
self.pageEdit.setText(str(self.currentPage))
self.recordQuery(index)
return


if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = BookStorageViewer()
mainMindow.show()
sys.exit(app.exec_())

代码分析#

  • 主要使用了 Qt自带的QTableViewQSqlQueryModel实现了查询和展示结果
  • 也有得到记录数,总页数,点击查询,跳转,前一页,后一页的函数,这些都很好看懂,同时还设置了按钮的可用性
  • 分页记录查询函数是这里面最主要的一个函数,首先看查询种类 ,分别有判断为空 ,模糊查询,查询无果的处理
  • 在跳转前有对输入数据的检测合法与否

这里还没有实现动态刷新,因为这个功能是用其他部件信号传输实现的

实现效果#

这里写图片描述
这里写图片描述
这里写图片描述

最后附上链接#

项目地址:github仓库链接

坚持原创技术分享,您的支持将鼓励我继续创作!