-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpesqaluno.py
108 lines (93 loc) · 4.28 KB
/
pesqaluno.py
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
# Form implementation generated from reading ui file 'pesqaluno.ui'
#
# Created by: PyQt6 UI code generator 6.5.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QTableWidgetItem
#Conexão com o BD
import mysql.connector
conexao = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="escola"
)
cursor = conexao.cursor()
print("Conectado ao BD.")
class Ui_PesqAluno(object):
def setupUi(self, PesqAluno):
PesqAluno.setObjectName("PesqAluno")
PesqAluno.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(parent=PesqAluno)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_nome = QtWidgets.QLabel(parent=self.centralwidget)
self.label_nome.setObjectName("label_nome")
self.horizontalLayout.addWidget(self.label_nome)
self.line_nome = QtWidgets.QLineEdit(parent=self.centralwidget)
self.line_nome.setObjectName("line_nome")
self.horizontalLayout.addWidget(self.line_nome)
self.botao_pesquisar = QtWidgets.QPushButton(parent=self.centralwidget)
self.botao_pesquisar.setObjectName("botao_pesquisar")
self.botao_pesquisar.clicked.connect(self.pesquisar)
self.horizontalLayout.addWidget(self.botao_pesquisar)
self.verticalLayout.addLayout(self.horizontalLayout)
self.tabela = QtWidgets.QTableWidget(parent=self.centralwidget)
self.tabela.setObjectName("tabela")
self.tabela.setColumnCount(4)
self.tabela.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tabela.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tabela.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tabela.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tabela.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tabela)
PesqAluno.setCentralWidget(self.centralwidget)
self.retranslateUi(PesqAluno)
QtCore.QMetaObject.connectSlotsByName(PesqAluno)
def retranslateUi(self, PesqAluno):
_translate = QtCore.QCoreApplication.translate
PesqAluno.setWindowTitle(_translate("PesqAluno", "Pesquisa de Aluno"))
self.label_nome.setText(_translate("PesqAluno", "Nome:"))
self.botao_pesquisar.setText(_translate("PesqAluno", "Pesquisar"))
item = self.tabela.horizontalHeaderItem(0)
item.setText(_translate("PesqAluno", "Código"))
item = self.tabela.horizontalHeaderItem(1)
item.setText(_translate("PesqAluno", "Nome"))
item = self.tabela.horizontalHeaderItem(2)
item.setText(_translate("PesqAluno", "Curso"))
item = self.tabela.horizontalHeaderItem(3)
item.setText(_translate("PesqAluno", "Turno"))
def pesquisar(self):
nome = self.line_nome.text() #captura o texto digitado
sql = "SELECT * FROM aluno WHERE nome LIKE %s"
cursor.execute(sql, ("%" + nome + "%",) )
dados = cursor.fetchall()
self.tabela.setRowCount(len(dados))
cont = 0 #contador de linhas
for linha in dados:
codigo = QTableWidgetItem(str(linha[0]))
nome = QTableWidgetItem(linha[1])
curso = QTableWidgetItem(linha[2])
turno = QTableWidgetItem(linha[3])
self.tabela.setItem(cont, 0, codigo)
self.tabela.setItem(cont, 1, nome)
self.tabela.setItem(cont, 2, curso)
self.tabela.setItem(cont, 3, turno)
cont += 1 #proxima linha
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
PesqAluno = QtWidgets.QMainWindow()
ui = Ui_PesqAluno()
ui.setupUi(PesqAluno)
PesqAluno.show()
sys.exit(app.exec())