现在的位置: 首页 > 数据库 > 正文

python和sqlite3数据库初探(简单登陆注册功能)

2019年08月06日 数据库 ⁄ 共 2405字 ⁄ 字号 评论关闭
#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()

抱歉!评论已关闭.