建立資料庫表:
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()