import tkinter as tk from tkinter import messagebox import mysql.connector def is_duplicated(商品表, 商品编号): connection = mysql.connector.connect(host="localhost", user="", passwd="", db="cus") cursor = connection.cursor() query = "SELECT 商品编号 FROM 商品表 " # SQL 插入语句 cursor.execute(query) results = cursor.fetchall() duplicated = False duplicates = [] for i in range(len(results)): for j in range(i + 1, len(results)): if results[i][0] == results[j][0]: duplicated = True duplicates.append(results[i][0]) break if duplicated: break cursor.close() connection.close() return duplicated ,duplicates def show_result(duplicated,duplicates): if duplicated: messagebox.showinfo("重复值存在", "数据库中存在重复值:\n"+"\n".join(duplicates)) else: messagebox.showinfo("无重复值", "数据库中不存在重复值") window = tk.Tk() window.title("判断数据库重复值") entry_table = tk.Entry(window) entry_table.grid(row=0, column=0) entry_column = tk.Entry(window) entry_column.grid(row=0, column=1) button = tk.Button(window, text="查询",command=lambda: show_result(*is_duplicated(entry_table.get(), entry_column.get()))) button.grid(row=1, column=1) window.mainloop()
python连接mysql判断某一列元素是否存在
最新推荐文章于 2024-07-12 16:16:27 发布