如何将ui表单连接到mysql并使用ui表单插入数据?

inkz8wg9  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(327)

我正在尝试将我的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_())

提前谢谢!

yqlxgs2m

yqlxgs2m1#

我在使用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_())

主窗口.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"))

相关问题