pythonとsqlite 3データベースの初期プローブ(簡単なログイン登録機能)
3693 ワード
#coding=utf8
#
import wx
import sqlite3
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'DB EXAMPLE',pos=wx.DefaultPosition,size=(300, 150))
panel = wx.Panel(self, -1)
usernameLabel = wx.StaticText(panel, -1, " :")# Label
self.usernameText = wx.TextCtrl(panel, -1, "",size=(175, -1))#
self.usernameText.SetInsertionPoint(0)
pwdLabel = wx.StaticText(panel, -1, " :")# Label
self.pwdText = wx.TextCtrl(panel, -1, "", size=(175, -1),style=wx.TE_PASSWORD)#
loginButton=wx.Button(panel,-1," ")#
exitButton=wx.Button(panel,-1," ")#
registerButton=wx.Button(panel,-1," ")
sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)#sizer
sizer.AddMany([usernameLabel, self.usernameText, pwdLabel, self.pwdText,loginButton,exitButton,registerButton])# sizer
panel.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnLogIn, loginButton)#
self.Bind(wx.EVT_BUTTON, self.OnCloseWindow, exitButton)#
self.Bind(wx.EVT_BUTTON, self.OnRegister, registerButton)#
# self.buildingDB()# , ,
def OnLogIn(self,event):#
self.username=self.usernameText.GetValue()
self.password=self.pwdText.GetValue()
username=str(self.username.strip())
conn=sqlite3.connect('db01')
cur=conn.cursor()
cur.execute("SELECT password FROM table01 WHERE username='%s'"% username)
t=cur.fetchone()[0]
print t
if str(self.password)==str(t):
print 'Password is correct!'
self.Maximize(True)# ,
else:
print 'failed'
def OnCloseWindow(self,event):#
self.Close()
# def loginmethod(self):
#
# pass
def buildingDB(self):#
conn=sqlite3.connect("db01")
cur=conn.cursor()
cur.execute("""
CREATE TABLE table01(username text,password text, realname text,account text,workingdept text,phonenumber text)
""")
cur.execute("""INSERT INTO table01 values('zhangsan','123','zhangsan','','','')""")
cur.execute("""INSERT INTO table01 values('lisi','123','zhangsan','','','')""")
cur.execute("""INSERT INTO table01 values('wangwu','123','zhangsan','','','')""")
conn.commit()
cur.execute("""SELECT username FROM table01 WHERE username='zhangsan'""")
# p=cur.fetchone()
# print p
cur.close()
def OnRegister(self,event):#
self.username=self.usernameText.GetValue()
self.password=self.pwdText.GetValue()
conn=sqlite3.connect("db01")
cur=conn.cursor()
cur.execute("INSERT INTO table01 VALUES('%s','%s','','','','')"%(self.username,self.password))
conn.commit()
print "Registered successfully!"
cur.close()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.MainLoop()