Python 如何修复mysqldb-pyqt5中的重复条目错误
我正在学习并尝试用pyqt5开发一个应用程序,它可以在我的数据库中插入数据。有时会键入现有的id号(主键),程序会。。。我已经创建了下一个代码:Python 如何修复mysqldb-pyqt5中的重复条目错误,python,pyqt,pyqt5,mysql-python,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):
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()
但产出保持不变。非常感谢您的帮助
id
自动递增列,您不需要键primary的值
cur.execute("INSERT INTO data (name, email, phone)"
"VALUES('%s','%s','%s')" % (
''.join(self.linedit1.text()),
''.join(self.linedit2.text()),
''.join(self.linedit3.text())))
也许可以在你的except子句中试试_mysql_exceptions.IntegrityError。谢谢你的回答;但是假设您,如果该值是您的社会保险标识,请为这些目的创建一个字段,例如
id\u social
。字段id
-主键(主键)是唯一索引的示例之一,用于唯一标识表项。这两个表项都不能具有相同的主键值。