Python鏈接SqlServer+GUI嵌入式——學生管理系統1.0

學生管理系統1.0html

1.建學生數據庫python

2.數據庫嵌入高級語言(Python)sql

3.界面設計數據庫

 

簡化思路:服務器

1.先經過SqlServer2012創建學生數據庫,包括帳號、密碼,姓名、選課等信息框架

2.運用Python的pymssql庫與SqlServer創建鏈接再用tkinter庫建立GUI界面ide

 

1)SqlServer建學生數據庫oop

 1 create database student_Mis  2 on  3 (name = student_Data,  4 filename='F:\SQL list\student_mis_data.mdf',  5 size = 20,  6 maxsize = 100,  7 filegrowth = 10)  8 log on  9 (name = student_Log, 10 filename = 'F:\SQL list\student_mis_log.ldf', 11 size = 5mb, 12 maxsize = 50mb, 13 filegrowth = 5mb)

2)在學生庫內建表並插入數據佈局

 1 create table Students  2 (學號 char(10) primary key,  3 密碼 varchar(20) not null,  4 姓名 char(20) not null,  5 性別 char(5),  6 出生日期 date  7 )  8  9 create table Course 10 (課程名 char(30) not null, 11 課程老師 char(20) not null, 12 課程號 char(10) primary key) 13 14 create table Report 15 (學號 char(10), 16 課程號 char(10), 17 成績 int, 18 primary key(學號,課程號), 19 constraint Students_Report foreign key(學號) references Students, 20 constraint Couse_Report foreign key(課程號) references Course) 21 22 23 insert 24 into Students 25 values 26 ('2018165201','12345678','李銘','','2000-02-01'), 27 ('2018165202','12345678','劉曉鳴','','2000-08-02'), 28 ('2018165203','12345678','李明','','1999-07-09'), 29 ('2018165204','12345678','張鷹','','1998-08-20'), 30 ('2018165205','12345678','劉竟靜','','1999-09-09'), 31 ('2018165206','12345678','劉成剛','','2000-10-10'), 32 ('2018165207','12345678','王銘','','2000-01-01'), 33 ('2018165118','12345678','右耳朵耗子','','1998-08-25') 34 35 insert 36 into Course 37 values 38 ('數據庫', '美美', '20185101'), 39 ('機率論', '楊曉東', '20185102'), 40 ('C語言', '黃曉明', '20185103'), 41 ('計算機基礎', '徐明', '20185104') 42 43 insert 44 into Report 45 values 46 ('2018165201','20185101', '90'), 47 ('2018165201','20185102', '80'), 48 ('2018165201','20185103', '99'), 49 ('2018165201','20185104', '88'), 50 51 ('2018165202','20185101', '70'), 52 ('2018165202','20185102', '90'), 53 ('2018165202','20185103', '90'), 54 ('2018165202','20185104', '78'), 55 56 ('2018165203','20185101', '89'), 57 ('2018165203','20185102', '85'), 58 ('2018165203','20185103', '90'), 59 ('2018165203','20185104', '67'), 60 61 ('2018165204','20185101', '88'), 62 ('2018165204','20185102', '85'), 63 ('2018165204','20185103', '85'), 64 ('2018165204','20185104', '85'), 65 66 ('2018165205','20185101', '90'), 67 ('2018165205','20185102', '90'), 68 ('2018165205','20185103', '90'), 69 ('2018165205','20185104', '90'), 70 71 ('2018165206','20185101', '0'), 72 ('2018165206','20185102', '0'), 73 ('2018165206','20185103', '0'), 74 ('2018165206','20185104', '0'), 75 76 ('2018165207','20185101', '0'), 77 ('2018165207','20185102', '0'), 78 ('2018165207','20185103', '0'), 79 ('2018165207','20185104', '0'), 80 ('2018165118','20185101', '100'), 81 ('2018165118','20185102', '100'), 82 ('2018165118','20185103', '100'), 83 ('2018165118','20185104', '100')

 

 3)Python創建鏈接並實現GUI界面fetch

 1 # ======================
 2 # imports
 3 # ======================
 4 
 5 from tkinter import *
 6 from tkinter import ttk  7 import pymssql  8 
 9 # import uuid
 10 # import _mssql
 11 # import decimal
 12 
 13 from tkinter import messagebox  14 class Basedesk():  15     """
 16  基準框模塊  17     """
 18     def __init__(self, master):  19 
 20 
 21         self.root = master  22  self.root.config()  23         self.root.title('教務管理系統')  24         self.width = 380  # 界面寬
 25         self.height = 300   # 界面高
 26         # 獲取屏幕尺寸以計算佈局參數,使窗口居屏幕中央
 27         self.screenwidth = self.root.winfo_screenwidth()  # 屏幕寬
 28         self.screenheight = self.root.winfo_screenheight()  # 屏幕高
 29         self.alignstr = '%dx%d+%d+%d' % (self.width, self.height, (self.screenwidth-self.width)/2, (self.screenheight-self.height)/2)  30  self.root.geometry(self.alignstr)  31         self.R = Register(self.root)  32  self.R.reigister(self.root)  33 
 34 
 35 class Register():  36 
 37     def __init__(self, master):  38         self.root = master  39         # 基準框架 """如下三行需根據我的實際修改"""
 40         self.ip = ' 填入我的ip地址 '
 41         self.id = ' 填入我的使用的SqlServer的帳號 '
 42         self.pd = ' 填入SqlServer帳號對應密碼 '
 43 
 44     """
 45  登陸模塊  46     """
 47     def reigister(self, master):  48         # 帳號密碼輸入框
 49         self.initface = LabelFrame(self.root, text='教務系統登陸', font=('微軟雅黑', 14))  50         self.initface.grid(padx=85, pady=30, )  51 
 52         self.people = Label(self.initface, text='帳號')  # 帳號
 53         self.people.grid(row=1, column=0, padx=10, pady=10)  54         self.password = Label(self.initface, text='密碼')   # 密碼
 55         self.password.grid(row=2, column=0, padx=10, pady=10)  56         self.var1 = StringVar  57         self.var2 = StringVar  58         self.entry_people = Entry(self.initface, textvariable=self.var1)   # 帳號輸入框
 59         self.entry_people.grid(row=1, column=1, padx=10, pady=10)  60         self.entry_password = Entry(self.initface, textvariable=self.var2, show='*')    # 密碼輸入框
 61         self.entry_password.grid(row=2, column=1, padx=10, pady=10)  62         self.button_into = Button(self.initface, text='登陸', command=self.conn)   # 登陸按鈕
 63         self.button_into.grid(row=3, column=0, padx=20, pady=20)  64         self.button_into = Button(self.initface, text='退出', command=self.root.quit)  # 退出按鈕
 65         self.button_into.grid(row=3, column=1, padx=20, pady=20)  66     def conn(self):  67         self.connect = pymssql.connect(self.ip, self.id, self.pd,  'student_Mis1')  # 服務器名,帳戶,密碼,數據庫名
 68         self.cursor = self.connect.cursor()  69         if self.connect:  70             print('鏈接成功')  71         self.sql = "select Students.學號,Students.密碼 from Students"
 72 
 73  self.cursor.execute(self.sql)  74         self.result = self.cursor.fetchone()  75         self.man = self.entry_people.get()  76         # self.pd = self.entry_password.get()
 77         while self.result:  78             print('%s|%s' % (self.result[0], self.result[1]))  79 
 80             if self.result[0] == self.entry_people.get() and self.result[1] == self.entry_password.get():  81                 print('帳號密碼正確')  82 
 83                 self.initface.destroy()  # 銷燬initface
 84  self.check()  85 
 86                 break
 87 
 88             else:  89 
 90                 self.result = self.cursor.fetchone()  91         else:  92             # 帳號或密碼錯誤清空輸入框
 93  self.entry_people.delete(0, END)  94  self.entry_password.delete(0, END)  95             messagebox.showinfo(title='提示', message='帳號或密碼輸入錯誤\n請從新輸入?')  96             # break
 97 
 98 
 99  self.cursor.close() 100  self.connect.close() 101 
102     """
103  選擇模塊 104     """
105     def check(self): 106         self.frame_checkbutton = LabelFrame(self.root, text='功能選擇', font=('微軟雅黑', 14)) 107         self.frame_checkbutton.grid(padx=60, pady=30) 108         # 查詢成績按鈕
109 
110         self.button_success = Button(self.frame_checkbutton, text='查詢成績', width=10, height=2, command=self.success) 111         self.button_success.grid(row=0, column=0, padx=20, pady=20) 112         # 修改密碼按鈕
113         self.button_revise = Button(self.frame_checkbutton, text='修改密碼', width=10, height=2, command=self.revise) 114         self.button_revise.grid(row=0, column=1, padx=20, pady=20) 115 
116     def success(self): 117 
118         # 鏈接數據庫
119         self.connect = pymssql.connect(self.ip, self.id, self.pd,  'student_Mis1')  # 服務器名,帳戶,密碼,數據庫名
120         if self.connect: 121             print('鏈接成功') 122             print(self.man) 123             # 查詢語句
124             search_sql = "select convert(nvarchar(20), 姓名) ,Students.學號,convert(nvarchar(20), 課程名) ,成績 from Students, Report, Course " \ 125                          "where Students.學號=Report.學號 and Report.課程號=Course.課程號 and Students.學號=%s" % self.man 126 
127             # 建立遊標
128             self.cursor1 = self.connect.cursor() 129  self.cursor1.execute(search_sql) 130             self.row = self.cursor1.fetchone()  # 讀取查詢結果
131 
132             # 表格框
133             root = Tk()   # 初始框的聲明
134             root.geometry('500x400+100+100') 135             root.title('成績查詢系統') 136             columns = ("姓名", "學號", "課程", "成績") 137             self.treeview = ttk.Treeview(root, height=18, show="headings", columns=columns) 138             self.treeview.column("姓名", width=150, anchor='center')  # 表示列,不顯示
139             self.treeview.column("學號", width=100, anchor='center') 140             self.treeview.column("課程", width=150, anchor='center') 141             self.treeview.column("成績", width=100, anchor='center') 142 
143             self.treeview.heading("姓名", text="姓名")  # 顯示錶頭
144             self.treeview.heading("學號", text="學號") 145             self.treeview.heading("課程", text="課程") 146             self.treeview.heading("成績", text="成績") 147             self.treeview.pack(side=LEFT, fill=BOTH) 148 
149             # 插入數據
150             while self.row: 151                 self.treeview.insert('', 0, values=(self.row[0], self.row[1], self.row[2], self.row[3])) 152                 self.row = self.cursor1.fetchone()  # 讀取查詢結果,
153 
154  self.cursor1.close() 155  self.connect.close() 156  root.mainloop() 157 
158 
159     def revise(self): 160         self.window = Tk()  # 初始框的聲明
161         self.window.geometry('400x200+100+100') 162         self.window.title('密碼修改管理') 163         self.frame_revise = LabelFrame(self.window) 164         self.frame_revise.grid(padx=60, pady=60) 165         self.label_revise = Label(self.frame_revise, text='新密碼:') 166         self.label_revise.grid(row=0, column=0, padx=10, pady=10) 167         self.var3 = StringVar 168         self.entry_revise = Entry(self.frame_revise, textvariable=self.var3) 169         self.entry_revise.grid(row=0, column=1, padx=10, pady=10) 170         self.button_ok = Button(self.frame_revise, text='肯定', command=self.ok) 171         self.button_ok.grid(row=1, column=0) 172         self.button_resive = Button(self.frame_revise, text='取消', command=self.resive) 173         self.button_resive.grid(row=1, column=1) 174         self.button_quit = Button(self.frame_revise, text='退出', command=self.window.destroy) 175         self.button_quit.grid(row=1, column=2) 176 
177     def ok(self): 178         # 鏈接數據庫
179         self.connect = pymssql.connect(self.ip, self.id, self.pd, 'student_Mis1')  # 服務器名,帳戶,密碼,數據庫名
180         self.cursor2 = self.connect.cursor()  # 建立遊標
181         sql_revise = "update Students set 密碼=%s where 學號=%s" % (self.entry_revise.get(), self.man) 182 
183         if self.connect: 184             print('鏈接成功') 185             print(self.man) 186  self.cursor2.execute(sql_revise) 187  self.connect.commit() 188             print(self.entry_revise.get()) 189             messagebox.showinfo(title='提示', message='密碼修改爲功!') 190  self.cursor2.close() 191  self.connect.close() 192 
193     def resive(self): 194  self.entry_revise.delete(0, END) 195 
196 if __name__ == '__main__': 197     root = Tk() 198  Basedesk(root) 199     mainloop()

效果圖:

1)登陸界面

 

 

2)功能選擇

 

 

3)成績查詢

 

 

4)密碼修改

 

 

 

缺點:1.功能較少,僅查詢成績與修改我的密碼。

   2.界面設計較簡陋。

      3.數據模式設計還不夠完整合理

說明:主要運用Python的pymssql和tkinter庫和類定義等方法作出這個嵌入式做業,將於空閒時間繼續改進,設計出更合理的數據關係模式。

原文出處:https://www.cnblogs.com/rightears-mouse/p/zqh001-python-sql.html

相關文章
相關標籤/搜索