我试图将PyQt表单连接到MySql数据库。我尝试使用以下代码连接到数据库并插入数据,但它不起作用。我错过了什么?我收到了属性错误__enter__
。这是什么意思?
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.QtCore import pyqtSlot
import mysql.connector as mdb
from mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.btnAdd.clicked.connect(self.InsertData)
@pyqtSlot()
def InsertData(self):
con = mdb.connect(user='root', password='password101',
host='localhost', database='sample_sys')
with con:
cur = con.cursor()
cur.execute("INSERT INTO name_info(name, age)"
"VALUES('%s','%s')" % (''.join(self.le_name.text()),
''.join(self.le_age.text())))
QMessageBox.information(self, "Connection", "Data Inserted Successfully")
self.close()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
提前谢谢!
答案 0 :(得分:0)
我使用MySQLdb,一切正常。 MySQLdb是流行的MySQL数据库服务器的线程兼容接口,提供Python数据库API。
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.QtCore import pyqtSlot
#import mysql.connector as mdb
import MySQLdb as mdb
from mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.button.setEnabled(False)
self.initWindow()
def initWindow(self):
self.lineedit1.setPlaceholderText('Please enter your name')
self.lineedit2.setPlaceholderText('Please enter your email')
self.lineedit3.setPlaceholderText('Please enter your phone number')
self.lineedit1.textChanged.connect(self.newText)
self.lineedit2.textChanged.connect(self.newText)
self.lineedit3.textChanged.connect(self.newText)
self.button.clicked.connect(self.InsertData)
@pyqtSlot()
def InsertData(self):
con = mdb.connect(host="localhost",user="user", passwd="password",db="testdb")
with con:
cur = con.cursor()
cur.execute('''INSERT INTO pyqt5data(name, email, phone)
VALUES (%s, %s, %s)''',
( self.lineedit1.text(),
self.lineedit2.text(),
self.lineedit3.text() )
)
cur.close()
QMessageBox.information(self, "Connection", "Data Inserted Successfully")
self.lineedit1.setText('')
self.lineedit2.setText('')
self.lineedit3.setText('')
self.initWindow()
def newText(self):
if self.lineedit1.text() and self.lineedit2.text() and self.lineedit3.text():
self.button.setEnabled(True)
else:
self.button.setEnabled(False)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
mainwindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.10
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 30, 81, 16))
self.label.setObjectName("label")
self.lineedit1 = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit1.setGeometry(QtCore.QRect(140, 30, 171, 20))
self.lineedit1.setObjectName("lineedit1")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(40, 70, 81, 16))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(40, 110, 81, 16))
self.label_3.setObjectName("label_3")
self.lineedit2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit2.setGeometry(QtCore.QRect(140, 70, 171, 20))
self.lineedit2.setObjectName("lineedit2")
self.lineedit3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineedit3.setGeometry(QtCore.QRect(140, 110, 171, 20))
self.lineedit3.setObjectName("lineedit3")
self.button = QtWidgets.QPushButton(self.centralwidget)
self.button.setGeometry(QtCore.QRect(94, 170, 181, 23))
self.button.setObjectName("button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Name"))
self.label_2.setText(_translate("MainWindow", "Email"))
self.label_3.setText(_translate("MainWindow", "Phone"))
self.button.setText(_translate("MainWindow", "INSERT"))