学生姓名录入查询

 

 

# -*- coding: utf-8 -*-
"""
Created on Sat Dec 11 20:09:13 2021

@author: ASUS
"""

import pymysql
import wx
class MyFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'学生学号录入与查询',size=(400,300))
panel = wx.Panel(self)
self.bt_input = wx.Button(panel,label='录入')
self.bt_input.Bind(wx.EVT_BUTTON,self.InputMessage)
self.bt_check = wx.Button(panel,label='查询')
self.bt_check.Bind(wx.EVT_BUTTON,self.CheckNum)
self.title = wx.StaticText(panel,label="请输入姓名与学号")
self.label_user = wx.StaticText(panel,label="姓名:")
self.text_user = wx.TextCtrl(panel,style=wx.TE_LEFT)
self.label_pwd = wx.StaticText(panel,label="学号:")
self.text_password = wx.TextCtrl(panel,style=wx.TE_LEFT)
hsizer_user = wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user,proportion=0,flag=wx.ALL,border=5)
hsizer_user.Add(self.text_user,proportion=1,flag=wx.ALL,border=5)
hsizer_pwd = wx.BoxSizer(wx.HORIZONTAL)
hsizer_pwd.Add(self.label_pwd,proportion=0,flag=wx.ALL,border=5)
hsizer_pwd.Add(self.text_password,proportion=1,flag=wx.ALL,border=5)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_input,proportion=0,flag=wx.ALIGN_CENTER,border=5)
hsizer_button.Add(self.bt_check,proportion=0,flag=wx.ALIGN_CENTER,border=5)
vsizer_all = wx.BoxSizer(wx.VERTICAL)
vsizer_all.Add(self.title,proportion=0,flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER,border=15)
vsizer_all.Add(hsizer_user,proportion=0,flag=wx.EXPAND | wx.LEFT | wx.RIGHT,border=45)
vsizer_all.Add(hsizer_pwd,proportion=0,flag=wx.EXPAND | wx.LEFT | wx.RIGHT,border=45)
vsizer_all.Add(hsizer_button,proportion=0,flag=wx.ALIGN_CENTER | wx.TOP,border=15)
panel.SetSizer(vsizer_all)
def InputMessage(self,event):
message=""
username = self.text_user.GetValue()
student_num = self.text_password.GetValue()
if username == "" or student_num =="":
message='姓名或学号不能为空'
else:
db=pymysql.connect(host="localhost",user="root",password="123456",database="mrsoft",charset="utf8")
cursor = db.cursor()
data=[(username,student_num),]
try:
cursor.executemany("insert into student(name,student_num) values(%s,%s)",data)
db.commit()
except:
db.rollback()
db.close()
message='录入成功'
wx.MessageBox(message)

def CheckNum(self,event):
message=""
username = self.text_user.GetValue()
student_num = self.text_password.GetValue()
if username == "" and student_num =="":
message='姓名和学号不能均为空'
elif username != "" and student_num =="":
db=pymysql.connect(host="localhost",user="root",password="123456",database="mrsoft",charset="utf8")
cursor = db.cursor()
sql = sql = "SELECT * FROM student \
WHERE name = '%s'" % username
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
name = row[1]
student_num = row[2]
message="学号:"+student_num
except:
message="Error: unable to fetch data"
elif username == "" and student_num!="":
db=pymysql.connect(host="localhost",user="root",password="123456",database="mrsoft",charset="utf8")
cursor = db.cursor()
sql = sql = "SELECT * FROM student \
WHERE student_num = %s" % (student_num)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
name = row[1]
student_num = row[2]
message="姓名:"+name
except:
message="Error: unable to fetch data"
wx.MessageBox(message)

if __name__ == '__main__':
app = wx.App()
frame = MyFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()

posted @ 2021-12-12 00:18  唐古利乌鸦  阅读(297)  评论(0)    收藏  举报