-------------------------------数据库的建立-------------------------------
import pymysql # 创建表格 db = pymysql.connect(host='localhost',user='root',password='13642205874lgw',database='mrsoft') cursor = db.cursor() cursor.execute('DROP TABLE IF EXISTS books') sqlm = ''' CREATE TABLE stu( class int(8) NOT NULL AUTO_INCREMENT, name varchar(50) NOT NULL, id int(8) NOT NULL NOT NULL, PRIMARY KEY(class) )ENGINE = MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ''' cursor.execute(sqlm) db.close() -------------------------------------主程序------------------------------------------- import wx import pymysql # GUI程序 class MyFrame(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'班级信息收集',size=(400,300)) #创建面板 panel = wx.Panel(self) #创建“保存”和“查询”按钮,并绑定事件 self.bt_storage = wx.Button(panel,label="保存") self.bt_storage.Bind(wx.EVT_BUTTON,self.OnclickSubmit) self.bt_inquire = wx.Button(panel,label ='查询') self.bt_inquire.Bind(wx.EVT_BUTTON,self.OnclickInquire) #创建文本,左对齐 self.title =wx.StaticText(panel,label ="班级信息保存查询") self.label_class =wx.StaticText(panel,label ="班级:") self.text_class =wx.TextCtrl(panel,style =wx.TE_LEFT) self.label_user =wx.StaticText(panel,label ="姓名:") self.text_user =wx.TextCtrl(panel,style =wx.TE_LEFT) self.label_number = wx.StaticText(panel,label ="学号:") self.text_number = wx.TextCtrl(panel,style =wx.TE_LEFT) #添加容器,容器中控件横向排列 hsizer_class =wx.BoxSizer(wx.HORIZONTAL) hsizer_class.Add(self.label_class,proportion=0,flag=wx.ALL,border=5) hsizer_class.Add(self.text_class,proportion=1,flag=wx.ALL,border=5) 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_number =wx.BoxSizer(wx.HORIZONTAL) hsizer_number.Add(self.label_number,proportion=0,flag=wx.ALL,border=5) hsizer_number.Add(self.text_number,proportion=1,flag=wx.ALL,border=5) hsizer_button =wx.BoxSizer(wx.HORIZONTAL) hsizer_button.Add(self.bt_storage,proportion=0,flag=wx.ALIGN_CENTER,border=5) hsizer_button.Add(self.bt_inquire,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_class,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) vsizer_all.Add(hsizer_user,proportion=0,flag=wx.EXPAND |wx.LEFT |wx.RIGHT,border=45) vsizer_all.Add(hsizer_number,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 OnclickSubmit(self,event): "单击保存按钮" #连接数据库 db =pymysql.connect(host='localhost',user='root',password='13642205874lgw',database="mrsoft",charset='utf8') message ="" Class =self.text_class.GetValue() #获取输入的班级 User =self.text_user.GetValue() #获取输入的用户名 Id =self.text_number.GetValue() #获取输入的学号 #使用cursor()方法创建一个游标对象 cursor = db.cursor() data = [(Class,User,Id)] try: #执行sql语句,插入数据 sql="insert into stu(class, name, id) values(%s,%s,%s)" cursor.executemany(sql,data) #提交数据 db.commit() except: #发生错误时回滚 db.rollback() #关闭连接 db.close() #判断是否为空 if Class =="" or User =="" or Id =="": message ='班级或名字或密码不能为空' else: message ='保存成功' #用户名或密码错误 wx.MessageBox(message) #弹出提示框 def OnclickInquire(self,event): "单击查询按钮" User =self.text_user.GetValue() #获取输入的用户名 Id =self.text_number.GetValue() #获取输入的学号 db =pymysql.connect(host='localhost',user='root',password='13642205874lgw',database="mrsoft",charset='utf8') #使用cursor()方法创建一个游标对象 cursor = db.cursor() sql ="" message ="" if User: sql ="SELECT * FROM stu \ WHERE NAME = %s" #找到该名字 try: #执行sql语句 cursor.execute(sql,User) #获取记录 results =cursor.fetchall() if results: #若找到不为空,输出对应的信息 for row in results: Class = row[0] User =row[1] Id =row[2] message ="班级:{}\n姓名:{}\n学号:{}\n".format(Class,User,Id) else: message ="没有这个名字" except: print("Error: unable to fecth data") #数据错误-- elif Id: sql ="SELECT * FROM stu \ WHERE NUMBER = %s" try: #执行sql语句 cursor.execute(sql,Id) #获取记录 results =cursor.fetchall() if results: for row in results: Class = row[0] User =row[1] Id =row[2] message ="班级:{}\n姓名:{}\n学号:{}\n".format(Class,User,Id) else: message ="没有这个学号" except: print("Error: unable to fecth data") else: message ="班级或名字或密码不能为空" #关闭数据库 db.close() wx.MessageBox(message) if __name__ == '__main__': app = wx.App() frame = MyFrame(parent=None,id=1) frame.Show() app.MainLoop()