1+
2+ '''
3+ Author: 木白广木林
4+ Date: 2024-05-18 08:19:40
5+ LastEditTime: 2024-05-18 09:32:30
6+ LastEditors: 木白广木林
7+ Description: None
8+ FilePath: \Desktop\db-SQLserver copy.py
9+ 检查自己的代码是非常愚蠢的行为,这是对本身实力的不信任。
10+ '''
11+ import sys
12+ import pyodbc
13+ import tkinter as tk
14+ from tkinter import messagebox
15+
16+ # 连接SQL Server数据库
17+ # conn = pyodbc.connect('DRIVER={SQL Server};SERVER=Your_Server_Name;DATABASE=Your_Database_Name;UID=Your_Username;PWD=Your_Password')
18+ conn = pyodbc .connect ('DRIVER={SQL Server};SERVER=DESKTOP-FCCVVU8;DATABASE=db;UID=sa;PWD=root' )
19+ cursor = conn .cursor ()
20+
21+ # def showMessage(message, type='info', timeout=2500):
22+ # import tkinter as tk
23+ # from tkinter import messagebox
24+
25+ # root = tk.Tk()
26+ # root.withdraw()
27+ # try:
28+ # root.after(timeout, root.destroy)
29+ # if type == 'info':
30+ # messagebox.showinfo('Info', message, master=root)
31+ # elif type == 'warning':
32+ # messagebox.showwarning('Warning', message, master=root)
33+ # elif type == 'error':
34+ # messagebox.showerror('Error', message, master=root)
35+ # except:
36+ # pass
37+
38+ # showMessage("Hello, world", timeout=1000)
39+
40+ # 查询数据库中是否已存在序列号
41+ def check_duplicate (serial ):
42+ cursor .execute ("SELECT * FROM number WHERE numbers = ?" , (serial ,))
43+ if cursor .fetchall ():
44+ return True
45+ else :
46+ return False
47+
48+ # 写入数据到数据库
49+ def insert_serial (entry ):
50+ serial = entry .get ()
51+ if len (serial ) != 6 :
52+ messagebox .showerror ("错误" , "序列号长度必须为6位" )
53+ elif check_duplicate (serial ):
54+ messagebox .showinfo ("提示" , f"序列号 { serial } 已存在,不重复写入数据库" )
55+ else :
56+ cursor .execute ("INSERT INTO number (numbers) VALUES (?)" , (serial ,))
57+ conn .commit ()
58+ messagebox .showinfo ("提示" , f"序列号 { serial } 已成功写入数据库" )
59+
60+
61+ # 创建UI界面
62+ def create_ui ():
63+
64+ def on_key_release (event ):
65+ # 获取文本框中的输入内容
66+ input_text = entry .get ()
67+ # 检查输入长度是否达到6个字符
68+ if len (input_text ) >= 6 :
69+ insert_serial (entry )
70+ print ("输入长度达到6个字符,写入数据。" )
71+ # 清空输入框
72+ entry .delete (0 , tk .END )
73+
74+
75+ root = tk .Tk ()
76+ root .title ("序列号查询软件" )
77+
78+ label = tk .Label (root , text = "手动输入序列号并查重写入数据库" )
79+ label .pack ()
80+
81+ entry = tk .Entry (root )
82+ entry .pack ()
83+
84+ # 绑定键盘释放事件到on_key_release函数
85+ entry .bind ('<KeyRelease>' , on_key_release )
86+
87+ button = tk .Button (root , text = "写入序列号" , command = lambda : insert_serial (entry ))
88+ button .pack ()
89+
90+ root .mainloop ()
91+
92+ # 主程序
93+ if __name__ == "__main__" :
94+ create_ui ()
95+
96+ # 关闭数据库连接
97+ cursor .close ()
98+ conn .close ()
0 commit comments