【发布时间】:2021-11-18 04:27:27
【问题描述】:
我想把 ErrorMessageBox 放在这个代码块上。就像它表明“数据已经存在”。
这是保存到数据库的代码块:
if (t1.get()=="" or t2.get()=="" or t3.get()=="" or t4.get()==""):
messagebox.showinfo("Result","Please Complete the Provided Details!")
这里连接到数据库和插入
else:
databases = mysql.connector.connect(
host ="localhost",
user = "userdata",
password = "",
database = "facerecog"
)
cursors = databases.cursor()
cursors.execute("SELECT * from record")
result = cursors.fetchall()
这是向数据库插入数据的代码
id= t4.get()
id = int(id)+1
id
sql = "INSERT INTO record(ids, names,course_year,positions) values(%s ,%s ,%s , %s)"
val = (id, t1.get(), t2.get(), t3.get())
cursors.execute(sql,val)
databases.commit()
如果数据已经存在,我想放 ErrorMessageBox。
在这段代码中拍照。此代码连接保存数据库。
face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def face_cropped(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
# scaling factor = 1.3
# minimum neighbor = 5
if faces is ():
return None
for (x,y,w,h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
cap = cv2.VideoCapture(0)
img_id = 0
while True:
ret, frame = cap.read()
if face_cropped(frame) is not None:
img_id+=1
face = cv2.resize(face_cropped(frame), (200,200))
face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
file_name_path = "img/"+str (t1.get())+"." +str(id)+"."+str(img_id)+".jpg"
cv2.imwrite(file_name_path, face)
cv2.putText(face, str(img_id), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow("Cropped face", face)
if cv2.waitKey(1)==1 or int(img_id)==50:
break
cap.release()
cv2.destroyAllWindows()
messagebox.showinfo("Result","Saved Completed!")
【问题讨论】:
-
您究竟需要在哪里执行验证?请您在您的代码中添加注释以了解如何为您提供帮助?
-
完成@sirandy 编辑
-
在
INSERT之前,您可能需要执行SELECT ... new_values来检查数据库中是否已有此数据。 -
如果它在消息框中显示代码错误怎么办?我的意思是它弹出消息框什么错误
-
你已经说出了你想要的,但你还没有提出任何问题。您需要帮助解决问题的哪一部分?
标签: python tkinter phpmyadmin localhost