建立数据库表:
import pymysql
db=pymysql.connect(host="localhost",user="root",password="ruirui",database="banji")
cursor=db.cursor()
cursor.execute("DROP TABLE IF EXISTS class")
sql="""
CREATE TABLE xinji (
class varchar(50) NOT NULL,
name varchar(50) NOT NULL,
number varchar(50) NOT NULL
)
"""
cursor.execute(sql)
db.close()
班级信息收集:
import pymysql
import wx
class MyFrame(wx.Frame):
def init(self,parent,id):
wx.Frame.init(self,parent,id,"班级信息收集",size=(430,300))
panel=wx.Panel(self)
self.bt_storage=wx.Button(panel,label='保存',pos=(120,170))
self.bt_storage.Bind(wx.EVT_BUTTON,self.OnclickStorage)
self.bt_inquire=wx.Button(panel,label='查询',pos=(215,170))
self.bt_inquire.Bind(wx.EVT_BUTTON,self.OnclickInquire)
self.title=wx.StaticText(panel,label="请输入以下信息进行保存或查询",pos=(130,20))
self.label_class=wx.StaticText(panel,label='班级:',pos=(50,50))
self.text_class=wx.TextCtrl(panel,pos=(100,50),size=(235,25),style=wx.TE_LEFT)
self.label_name=wx.StaticText(panel,label='姓名:',pos=(50,90))
self.text_name=wx.TextCtrl(panel,pos=(100,90),size=(235,25),style=wx.TE_LEFT)
self.label_number=wx.StaticText(panel,label='学号:',pos=(50,130))
self.text_number=wx.TextCtrl(panel,pos=(100,130),size=(235,25),style=wx.TE_LEFT)
def OnclickStorage(self,event):
"""单击保存按钮,执行方法"""
db=pymysql.connect(host="localhost",user="root",password="ruirui",database="banji",charset='utf8')
message=""
classname=self.text_class.GetValue()
name=self.text_name.GetValue()
number=self.text_number.GetValue()
cursor=db.cursor()
if classname=="" or name=="" or number=="":
message="班级或姓名或学号不能为空"
else:
data=[(classname,name,number)]
try:
sql="insert into xinji(class,name,number) values(%s,%s,%s)"
cursor.executemany(sql,data)
db.commit()
message="保存成功"
except:
db.rollback()
db.close()
wx.MessageBox(message)
def OnclickInquire(self,event):
"""单击查询按钮,执行方法"""
classname_1=self.text_class.GetValue()
name_1=self.text_name.GetValue()
number_1=self.text_number.GetValue()
db=pymysql.connect(host="localhost",user="root",password="ruirui",database="banji")
cursor=db.cursor()
message=""
sql=""
if name_1!="" and number_1!="":
sql="select * from xinji where number= %s"
try:
cursor.execute(sql,number_1)
result=cursor.fetchall()
if result:
for row in result:
classname=row[0]
name=row[1]
number=row[2]
if name==name_1:
message="班级:{}\n姓名:{}\n学号:{}\n".format(classname,name,number)
else:
message="姓名与学号不匹配"
except:
message="没有该学号"
elif name_1!="" and number_1 =="":
sql="select * from xinji where name=%s"
try:
cursor.execute(sql,name_1)
result=cursor.fetchall()
for row in result:
classname=row[0]
name=row[1]
number=row[2]
message="班级:{}\n姓名:{}\n学号:{}\n".format(classname,name,number)
except:
message="没有这个名字"
elif number_1!="" and name_1=="":
sql="select * from xinji where number=%s"
try:
cursor.execute(sql,number_1)
result=cursor.fetchall()
for row in result:
classname=row[0]
name=row[1]
number=row[2]
message="班级:{}\n姓名:{}\n学号:{}\n".format(classname,name,number)
except:
message="没有这个学号"
else:
message="班级或姓名或学号不能为空"
db.close()
wx.MessageBox(message)
if name == 'main':
app=wx.App()
frame=MyFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()