如何修复mysqldb-pyqt5中的重复条目错误

时间:2018-08-29 23:43:19

标签: python pyqt pyqt5 mysql-python

我正在学习并尝试使用pyqt5生成一个应用程序,该程序将数据插入到我的数据库中。有时会键入一个现有的ID号(主键),并且程序会掉落...我创建了下一个代码:

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, 
QPushButton, QMessageBox
import sys
import MySQLdb as mdb 

class Window(QMainWindow):

  def __init__(self):
    super().__init__()

    self.title = "PyQt5 Insert Data"        
    self.top = 100        
    self.left = 100        
    self.width = 680        
    self.height = 400
    self.setWindowIcon(QtGui.QIcon("icon_health.png"))

    self.InitWindow()

  def InitWindow(self):

    self.linedit0 = QLineEdit(self)
    self.linedit0.setPlaceholderText('Please enter your ID')
    self.linedit0.setGeometry(200,50,200,30)

    self.linedit1 = QLineEdit(self)
    self.linedit1.setPlaceholderText('Please enter your name')
    self.linedit1.setGeometry(200,100,200,30)

    self.linedit2 = QLineEdit(self)
    self.linedit2.setPlaceholderText('Please enter your email')
    self.linedit2.setGeometry(200,150,200,30)

    self.linedit3 = QLineEdit(self)
    self.linedit3.setPlaceholderText('Please enter your phone')
    self.linedit3.setGeometry(200,200,200,30)

    self.button = QPushButton("Insert data", self)
    self.button.setGeometry(200,250,100,30)
    self.button.clicked.connect(self.Insertdata)

    self.setWindowIcon(QtGui.QIcon("icon_health.png"))
    self.setWindowTitle(self.title)
    self.setGeometry(self.top, self.left, self.width, self.height)
    self.show()

 def Insertdata (self):
    con = mdb.connect("localhost","root","xxxx","dbname")
    with con:
        cur = con.cursor()
        cur.execute("INSERT INTO data (id, name, email, phone)" 
                     "VALUES('%s','%s','%s','%s')" % (''.join(self.linedit0.text()),
                                                 ''.join(self.linedit1.text()),
                                                 ''.join(self.linedit2.text()),
                                                 ''.join(self.linedit3.text())))
        QMessageBox.about(self, "Connection","Data Inserted Sucessfully")
        self.close()

App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

该应用程序运行良好,但是当我键入重复的ID时,请输入(主键)程序会掉下来并进行广告:

Traceback (most recent call last):
File "C:\Users\oscar\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\oscar\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\oscar\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\oscar\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\oscar\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\oscar\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.IntegrityError: (1062, "Duplicate entry '#######' for key 
'PRIMARY'")
[Finished in 13.2s]

对我来说这真是一个挫败感。我有一个解决方案:

try:
   con = mdb.connect("localhost", "root", "xxxxx", "dbname") 
except _mysql.Error as e: 
   QMessageBox.about(self,"Connection", "Duplicate entry for ID")
   sys.exit()

但是输出保持不变。我会感谢您的帮助

1 个答案:

答案 0 :(得分:0)

id的自动递增列,您不需要主要键的值。

cur.execute("INSERT INTO data (name, email, phone)" 
                     "VALUES('%s','%s','%s')" % (
                                                 ''.join(self.linedit1.text()),
                                                 ''.join(self.linedit2.text()),
                                                 ''.join(self.linedit3.text())))