I'm a beginner in python and I'm trying to develop my first application in this language and I'm having trouble writing information to a mysql table inside the phpMyAdmin tool, the situation is as follows, I created a form in PYQT4 DESIGNER and converted to python, this form was called "frmEscolas" and has the following encoding.
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(1005, 250)
self.lblCod = QtGui.QLabel(Dialog)
self.lblCod.setGeometry(QtCore.QRect(10, 40, 51, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblCod.setFont(font)
self.lblCod.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.lblCod.setObjectName(_fromUtf8("lblCod"))
self.edtCodigo = QtGui.QLineEdit(Dialog)
self.edtCodigo.setGeometry(QtCore.QRect(70, 40, 111, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtCodigo.setFont(font)
self.edtCodigo.setFocusPolicy(QtCore.Qt.NoFocus)
self.edtCodigo.setAcceptDrops(False)
self.edtCodigo.setDragEnabled(False)
self.edtCodigo.setReadOnly(False)
self.edtCodigo.setObjectName(_fromUtf8("edtCodigo"))
self.lblEscola = QtGui.QLabel(Dialog)
self.lblEscola.setGeometry(QtCore.QRect(190, 40, 51, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblEscola.setFont(font)
self.lblEscola.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblEscola.setObjectName(_fromUtf8("lblEscola"))
self.edtEscola = QtGui.QLineEdit(Dialog)
self.edtEscola.setGeometry(QtCore.QRect(250, 40, 491, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtEscola.setFont(font)
self.edtEscola.setFocusPolicy(QtCore.Qt.StrongFocus)
self.edtEscola.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.edtEscola.setAcceptDrops(True)
self.edtEscola.setAutoFillBackground(False)
self.edtEscola.setEchoMode(QtGui.QLineEdit.Normal)
self.edtEscola.setCursorPosition(0)
self.edtEscola.setDragEnabled(False)
self.edtEscola.setReadOnly(False)
self.edtEscola.setCursorMoveStyle(QtCore.Qt.LogicalMoveStyle)
self.edtEscola.setObjectName(_fromUtf8("edtEscola"))
self.lblAbrevia = QtGui.QLabel(Dialog)
self.lblAbrevia.setGeometry(QtCore.QRect(750, 40, 91, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblAbrevia.setFont(font)
self.lblAbrevia.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblAbrevia.setObjectName(_fromUtf8("lblAbrevia"))
self.edtAbreviat = QtGui.QLineEdit(Dialog)
self.edtAbreviat.setGeometry(QtCore.QRect(850, 40, 141, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtAbreviat.setFont(font)
self.edtAbreviat.setAlignment(QtCore.Qt.AlignCenter)
self.edtAbreviat.setObjectName(_fromUtf8("edtAbreviat"))
self.lblNro = QtGui.QLabel(Dialog)
self.lblNro.setGeometry(QtCore.QRect(820, 80, 61, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblNro.setFont(font)
self.lblNro.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblNro.setObjectName(_fromUtf8("lblNro"))
self.lblCep = QtGui.QLabel(Dialog)
self.lblCep.setGeometry(QtCore.QRect(230, 80, 41, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblCep.setFont(font)
self.lblCep.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblCep.setObjectName(_fromUtf8("lblCep"))
self.edtCep = QtGui.QLineEdit(Dialog)
self.edtCep.setGeometry(QtCore.QRect(280, 80, 91, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtCep.setFont(font)
self.edtCep.setObjectName(_fromUtf8("edtCep"))
self.edtEnd = QtGui.QLineEdit(Dialog)
self.edtEnd.setGeometry(QtCore.QRect(460, 80, 351, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtEnd.setFont(font)
self.edtEnd.setObjectName(_fromUtf8("edtEnd"))
self.lblPolo = QtGui.QLabel(Dialog)
self.lblPolo.setGeometry(QtCore.QRect(10, 80, 41, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblPolo.setFont(font)
self.lblPolo.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.lblPolo.setObjectName(_fromUtf8("lblPolo"))
self.edtNro = QtGui.QLineEdit(Dialog)
self.edtNro.setGeometry(QtCore.QRect(890, 80, 101, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtNro.setFont(font)
self.edtNro.setObjectName(_fromUtf8("edtNro"))
self.cboPolo = QtGui.QComboBox(Dialog)
self.cboPolo.setGeometry(QtCore.QRect(50, 80, 171, 22))
font = QtGui.QFont()
font.setPointSize(11)
self.cboPolo.setFont(font)
self.cboPolo.setFocusPolicy(QtCore.Qt.StrongFocus)
self.cboPolo.setAcceptDrops(True)
self.cboPolo.setEditable(True)
self.cboPolo.setInsertPolicy(QtGui.QComboBox.InsertAfterCurrent)
self.cboPolo.setObjectName(_fromUtf8("cboPolo"))
self.lblEndereco = QtGui.QLabel(Dialog)
self.lblEndereco.setGeometry(QtCore.QRect(380, 80, 71, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblEndereco.setFont(font)
self.lblEndereco.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblEndereco.setObjectName(_fromUtf8("lblEndereco"))
self.checkBox = QtGui.QCheckBox(Dialog)
self.checkBox.setGeometry(QtCore.QRect(410, 10, 101, 17))
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox.setFont(font)
self.checkBox.setObjectName(_fromUtf8("checkBox"))
self.checkBox_2 = QtGui.QCheckBox(Dialog)
self.checkBox_2.setGeometry(QtCore.QRect(510, 10, 101, 17))
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox_2.setFont(font)
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.radioButton = QtGui.QRadioButton(Dialog)
self.radioButton.setGeometry(QtCore.QRect(250, 10, 61, 17))
font = QtGui.QFont()
font.setPointSize(10)
self.radioButton.setFont(font)
self.radioButton.setObjectName(_fromUtf8("radioButton"))
self.radioButton_2 = QtGui.QRadioButton(Dialog)
self.radioButton_2.setGeometry(QtCore.QRect(320, 10, 61, 17))
font = QtGui.QFont()
font.setPointSize(10)
self.radioButton_2.setFont(font)
self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))
self.edtBairro = QtGui.QLineEdit(Dialog)
self.edtBairro.setGeometry(QtCore.QRect(680, 120, 311, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtBairro.setFont(font)
self.edtBairro.setAlignment(QtCore.Qt.AlignCenter)
self.edtBairro.setObjectName(_fromUtf8("edtBairro"))
self.edtComp = QtGui.QLineEdit(Dialog)
self.edtComp.setGeometry(QtCore.QRect(120, 120, 491, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtComp.setFont(font)
self.edtComp.setFocusPolicy(QtCore.Qt.StrongFocus)
self.edtComp.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.edtComp.setAcceptDrops(True)
self.edtComp.setAutoFillBackground(False)
self.edtComp.setEchoMode(QtGui.QLineEdit.Normal)
self.edtComp.setCursorPosition(0)
self.edtComp.setDragEnabled(False)
self.edtComp.setReadOnly(False)
self.edtComp.setCursorMoveStyle(QtCore.Qt.LogicalMoveStyle)
self.edtComp.setObjectName(_fromUtf8("edtComp"))
self.lblBairro = QtGui.QLabel(Dialog)
self.lblBairro.setGeometry(QtCore.QRect(620, 120, 51, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblBairro.setFont(font)
self.lblBairro.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblBairro.setObjectName(_fromUtf8("lblBairro"))
self.lblComplem = QtGui.QLabel(Dialog)
self.lblComplem.setGeometry(QtCore.QRect(10, 120, 101, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblComplem.setFont(font)
self.lblComplem.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.lblComplem.setObjectName(_fromUtf8("lblComplem"))
self.edtPolo_2 = QtGui.QLineEdit(Dialog)
self.edtPolo_2.setGeometry(QtCore.QRect(340, 160, 41, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtPolo_2.setFont(font)
self.edtPolo_2.setAlignment(QtCore.Qt.AlignCenter)
self.edtPolo_2.setObjectName(_fromUtf8("edtPolo_2"))
self.lblCidade = QtGui.QLabel(Dialog)
self.lblCidade.setGeometry(QtCore.QRect(10, 160, 61, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblCidade.setFont(font)
self.lblCidade.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.lblCidade.setObjectName(_fromUtf8("lblCidade"))
self.lineEdit_7 = QtGui.QLineEdit(Dialog)
self.lineEdit_7.setGeometry(QtCore.QRect(440, 160, 141, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.lineEdit_7.setFont(font)
self.lineEdit_7.setObjectName(_fromUtf8("lineEdit_7"))
self.lblFone = QtGui.QLabel(Dialog)
self.lblFone.setGeometry(QtCore.QRect(390, 160, 41, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblFone.setFont(font)
self.lblFone.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblFone.setObjectName(_fromUtf8("lblFone"))
self.lineEdit_5 = QtGui.QLineEdit(Dialog)
self.lineEdit_5.setGeometry(QtCore.QRect(70, 160, 231, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.lineEdit_5.setFont(font)
self.lineEdit_5.setObjectName(_fromUtf8("lineEdit_5"))
self.lblUf = QtGui.QLabel(Dialog)
self.lblUf.setGeometry(QtCore.QRect(300, 160, 31, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblUf.setFont(font)
self.lblUf.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblUf.setObjectName(_fromUtf8("lblUf"))
self.edtNroAlun = QtGui.QLineEdit(Dialog)
self.edtNroAlun.setGeometry(QtCore.QRect(690, 160, 81, 20))
font = QtGui.QFont()
font.setPointSize(11)
self.edtNroAlun.setFont(font)
self.edtNroAlun.setObjectName(_fromUtf8("edtNroAlun"))
self.lblNro_2 = QtGui.QLabel(Dialog)
self.lblNro_2.setGeometry(QtCore.QRect(590, 160, 91, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblNro_2.setFont(font)
self.lblNro_2.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblNro_2.setObjectName(_fromUtf8("lblNro_2"))
self.edtDtCadast = QtGui.QDateEdit(Dialog)
self.edtDtCadast.setGeometry(QtCore.QRect(900, 160, 91, 22))
self.edtDtCadast.setLayoutDirection(QtCore.Qt.LeftToRight)
self.edtDtCadast.setCorrectionMode(QtGui.QAbstractSpinBox.CorrectToPreviousValue)
self.edtDtCadast.setCurrentSection(QtGui.QDateTimeEdit.MonthSection)
self.edtDtCadast.setCalendarPopup(True)
self.edtDtCadast.setTimeSpec(QtCore.Qt.LocalTime)
self.edtDtCadast.setObjectName(_fromUtf8("edtDtCadast"))
self.lblNro_3 = QtGui.QLabel(Dialog)
self.lblNro_3.setGeometry(QtCore.QRect(780, 160, 111, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.lblNro_3.setFont(font)
self.lblNro_3.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTop|QtCore.Qt.AlignTrailing)
self.lblNro_3.setObjectName(_fromUtf8("lblNro_3"))
self.btnGravar = QtGui.QPushButton(Dialog)
self.btnGravar.setGeometry(QtCore.QRect(320, 210, 71, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.btnGravar.setFont(font)
self.btnGravar.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.btnGravar.setMouseTracking(False)
self.btnGravar.setObjectName(_fromUtf8("btnGravar"))
self.btnPesquisar = QtGui.QPushButton(Dialog)
self.btnPesquisar.setGeometry(QtCore.QRect(460, 210, 111, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.btnPesquisar.setFont(font)
self.btnPesquisar.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.btnPesquisar.setMouseTracking(False)
self.btnPesquisar.setObjectName(_fromUtf8("btnPesquisar"))
self.line = QtGui.QFrame(Dialog)
self.line.setGeometry(QtCore.QRect(10, 190, 981, 20))
self.line.setFrameShape(QtGui.QFrame.HLine)
self.line.setFrameShadow(QtGui.QFrame.Sunken)
self.line.setObjectName(_fromUtf8("line"))
self.btnFechar = QtGui.QPushButton(Dialog)
self.btnFechar.setGeometry(QtCore.QRect(630, 210, 71, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.btnFechar.setFont(font)
self.btnFechar.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.btnFechar.setMouseTracking(False)
self.btnFechar.setObjectName(_fromUtf8("btnFechar"))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.lblCod.setText(_translate("Dialog", "Código", None))
self.lblEscola.setText(_translate("Dialog", "Escola", None))
self.lblAbrevia.setText(_translate("Dialog", "Abreviatura", None))
self.lblNro.setText(_translate("Dialog", "Número", None))
self.lblCep.setText(_translate("Dialog", "CEP ", None))
self.lblPolo.setText(_translate("Dialog", "Pólo", None))
self.lblEndereco.setText(_translate("Dialog", "Endereço", None))
self.checkBox.setText(_translate("Dialog", "Zona Urbana", None))
self.checkBox_2.setText(_translate("Dialog", "Zona Urbana", None))
self.radioButton.setText(_translate("Dialog", "Escola", None))
self.radioButton_2.setText(_translate("Dialog", "Creche", None))
self.lblBairro.setText(_translate("Dialog", "Bairro", None))
self.lblComplem.setText(_translate("Dialog", "Complemento", None))
self.lblCidade.setText(_translate("Dialog", "Cidade ", None))
self.lblFone.setText(_translate("Dialog", "Fone", None))
self.lblUf.setText(_translate("Dialog", "UF", None))
self.lblNro_2.setText(_translate("Dialog", "Nro. Alunos", None))
self.lblNro_3.setText(_translate("Dialog", "Data Cadastro", None))
self.btnGravar.setText(_translate("Dialog", "Gravar", None))
self.btnPesquisar.setText(_translate("Dialog", "Pesquisar", None))
self.btnFechar.setText(_translate("Dialog", "Fechar", None))
I created another form called "frmChamaSchool" which is where I am trying to make the connection and save the data entered in the form above and has the following code.
import sys
import MySQLdb
from PyQt4 import QtGui, QtCore
from frmEscolas import Ui_Dialog
def conexaoBanco():
BANCO="siscomedb"
USER="root"
PASSWD=""
HOST="127.0.0.1"
try:
con = MySQLdb.connect(db=BANCO,user=USER,passwd=PASSWD,host=HOST)
print("Conseguiu conectar ao banco de dados")
except MySQLdb as erro:
print("Não conseguiu conectar ao banco de dados.: ",erro)
return con
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.btnGravar.clicked.connect(self.cadastro)
def cadastro(self):
self.conexao = conexaoBanco()
self.nomescola = self.ui.edtEscola.text()
self.abescola = self.ui.edtAbreviat.text()
#self.codpolo = self.ui.cboPolo.text()
self.cep = self.ui.edtCep.text()
self.endereco = self.ui.edtEnd.text()
self.numero = self.ui.edtNro.text()
self.complemento = self.ui.edtComp.text()
self.bairro = self.ui.edtBairro.text()
self.cidade = self.ui.lineEdit_5.text()
self.uf = self.ui.lineEdit_7.text()
self.fone = self.ui.edtPolo_2.text()
self.nroalunos = self.ui.edtNroAlun.text()
self.dtcadastro = self.ui.edtDtCadast.text()
self.escsegmento = self.ui.radioButton.text()
self.zona = self.ui.checkBox.text()
cursor = conexao.cursor()
sql="insert into tblescolas (nomescola,abescola,cep,endereco,numero,complemento,bairro,cidade,uf,fone,nroalunos) values('"+nomescola+"','"+abescola+"','"+cep+"','"+endereco+"','"+numero+"','"+complemento+"','"+bairro+"','"+cidade+"','"+uf+"','"+fone+"','"+nroalunos+"')"
try:
self.cursor.execute(sql)
self.conexao.commit()
print("Dados gravados com sucesso")
#limparcampos()
except MySQLdb as erro:
print("Não conseguiu cadastrar no banco de dados.: ",erro)
self.conexao.close()
def mostra(self):
self.ui.edtEscola.setText("Você clicou no botão gravar")
app = QtGui.QApplication(sys.argv)
programa=Main()
programa.show()
sys.exit(app.exec_())
When I click the "Save" button, I get the following error.
Traceback (most recent call last):
File "C:\ProjetosPython\Siscome\frmChamaEscolas.py", line 52, in cadastro
cursor = conexao.cursor()
NameError: global name 'conexao' is not defined