赞
踩
在上一篇文章中,我们谈到了关于PyQt5连接mysql查询所有数据,并显示在table Widget表格,那么问题来了
提示:以下是本篇文章正文内容,下面案例可供参考
如果我们就想获取数据库表格中的某一行数据该怎么办呢?
1.先生成基本页面,页面将采用Combo Box即下拉框的形式;
2.给按钮添加信号连接功能(即创建按钮函数);
3.连接mysql并查询数据库,返回的是二维元组;
4.遍历数据库表格中的每一行二维元组,并将数据显示在table Widget里。
视频演示地址连接:
PyQt5连接mysql查询某一行数据 并显示在table
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QWidget import sys import pymysql class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(911, 670) self.comboBox = QtWidgets.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(301, 104, 161, 21)) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.setItemText(0, "") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(469, 101, 93, 28)) self.pushButton.setObjectName("pushButton") self.tableWidget = QtWidgets.QTableWidget(Form) self.tableWidget.setGeometry(QtCore.QRect(230, 140, 381, 401)) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(3) self.tableWidget.setRowCount(13) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(4, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(5, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(6, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(7, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(8, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(9, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(10, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(11, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(12, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.comboBox.setItemText(1, _translate("Form", "沈阳理工大学")) self.comboBox.setItemText(2, _translate("Form", "东北大学")) self.comboBox.setItemText(3, _translate("Form", "大连理工大学")) self.comboBox.setItemText(4, _translate("Form", "沈阳工业大学")) self.comboBox.setItemText(5, _translate("Form", "大连海洋大学")) self.comboBox.setItemText(6, _translate("Form", "辽宁科技大学")) self.comboBox.setItemText(7, _translate("Form", "辽宁师范大学")) self.comboBox.setItemText(8, _translate("Form", "沈阳建筑大学")) self.comboBox.setItemText(9, _translate("Form", "辽宁工程技术大学")) self.comboBox.setItemText(10, _translate("Form", "沈阳航空航天大学")) self.comboBox.setItemText(11, _translate("Form", "大连交通大学")) self.comboBox.setItemText(12, _translate("Form", "大连海事大学")) self.pushButton.setText(_translate("Form", "PushButton")) item = self.tableWidget.verticalHeaderItem(0) item.setText(_translate("Form", "1")) item = self.tableWidget.verticalHeaderItem(1) item.setText(_translate("Form", "2")) item = self.tableWidget.verticalHeaderItem(2) item.setText(_translate("Form", "3")) item = self.tableWidget.verticalHeaderItem(3) item.setText(_translate("Form", "4")) item = self.tableWidget.verticalHeaderItem(4) item.setText(_translate("Form", "5")) item = self.tableWidget.verticalHeaderItem(5) item.setText(_translate("Form", "6")) item = self.tableWidget.verticalHeaderItem(6) item.setText(_translate("Form", "7")) item = self.tableWidget.verticalHeaderItem(7) item.setText(_translate("Form", "8")) item = self.tableWidget.verticalHeaderItem(8) item.setText(_translate("Form", "9")) item = self.tableWidget.verticalHeaderItem(9) item.setText(_translate("Form", "10")) item = self.tableWidget.verticalHeaderItem(10) item.setText(_translate("Form", "11")) item = self.tableWidget.verticalHeaderItem(11) item.setText(_translate("Form", "12")) item = self.tableWidget.verticalHeaderItem(12) item.setText(_translate("Form", "13")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("Form", "Number")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("Form", "University")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("Form", "Award")) if __name__ == '__main__': app = QApplication(sys.argv) Widget = QWidget() ui = Ui_Form() ui.setupUi(Widget) Widget.show() sys.exit(app.exec_()
self.pushButton.clicked.connect(self.load)
def load(self):
conn = pymysql.Connect(
host='localhost',
user='root',
password='自己设置的mysql密码',
database='mysql'
)
cursor = conn.cursor()
yousearch_content = self.comboBox.currentText() if yousearch_content == "沈阳理工大学": cursor.execute('select * from awardList') result = cursor.fetchall() elif yousearch_content == "东北大学": cursor.execute('select * from awardList limit 1,1') result = cursor.fetchall() elif yousearch_content == "大连理工理工大学": cursor.execute('select * from awardList limit 2,1') result = cursor.fetchall() elif yousearch_content == "沈阳工业大学": cursor.execute('select * from awardList limit 3,1') result = cursor.fetchall() elif yousearch_content == "大连海洋大学": cursor.execute('select * from awardList limit 4,1') result = cursor.fetchall() elif yousearch_content == "辽宁科技大学": cursor.execute('select * from awardList limit 5,1') result = cursor.fetchall() elif yousearch_content == "辽宁师范大学": cursor.execute('select * from awardList limit 6,1') result = cursor.fetchall() elif yousearch_content == "沈阳建筑大学": cursor.execute('select * from awardList limit 7,1') result = cursor.fetchall() elif yousearch_content == "辽宁工程技术大学": cursor.execute('select * from awardList limit 8,1') result = cursor.fetchall() elif yousearch_content == "沈阳航空航天大学": cursor.execute('select * from awardList limit 9,1') result = cursor.fetchall() elif yousearch_content == "大连交通大学": cursor.execute('select * from awardList limit 10,1') result = cursor.fetchall() elif yousearch_content == "大连海事大学": cursor.execute('select * from awardList limit 11,1') result = cursor.fetchall() else: result = "无查询信息!" print(result) print(result[0][0]) self.tableWidget.setItem(0,0,QtWidgets.QTableWidgetItem(str(result[0][0]))) self.tableWidget.setItem(0,1, QtWidgets.QTableWidgetItem(result[0][1])) self.tableWidget.setItem(0, 2, QtWidgets.QTableWidgetItem(result[0][2])) cursor.close() conn.close()
上述同时也用到了条件if语句,本篇内容比较综合,需慢慢消化!点击下拉框中某一学校,数据显示如图所示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。