第五天上-面向对象思想mysql+python


前提条件:
1.在昨天 的数据库scuecdb中创建一张用户表
create table userinfo
(
userid char(20) primary key,
userpwd char(20)
);

2.在pycharm里面用OOP来实现注册
先PACKAGE三个模块
domain(对象)
dao(编程)
test(调用方法的界面)
# domain

class Userinfo:
    def __init__(self,userid,userpwd):
        self.userid = userid
        self.userpwd = userpwd

    def __str__(self):
        return self.userid + self.userpwd
class Userinfo:
    def __init__(self,userid,userpwd):
        self.userid = userid
        self.userpwd = userpwd

    def __str__(self):
        return self.userid + self.userpwd

#dao

import pymysql

class UserinfoDao:
    def userinfoRegister(self,userinfo):
        conn = pymysql.connect("localhost","root","2611","scuecdb")

        cur = conn.cursor()

        insertSQL = "insert into userinfo(userid,userpwd) values('"+userinfo.userid+"','"+userinfo.userpwd+"')"
        count= cur.execute(insertSQL)

        conn.commit()

        conn.close()

        if(count>0):
            return True
        else:
            return False

    def userinfoLogin(self, userinfo):
        conn = pymysql.connect("localhost", "root", "2611", "scuecdb")

        cur = conn.cursor()

        loginSQL = "select userid from userinfo where userid='" + userinfo.userid + "' and userpwd='"+userinfo.userpwd+"'"

        count = cur.execute(loginSQL)

        conn.close()

        if count > 0:
            return True
        else:
            return False
#test
from domain import Userinfo
from dao import UserinfoDao

def main():
      while(True):
          print("中南民族大学会员管理系统V1.0")
          choice=int(input("1.注册...\n2.登陆...\n3.退出..\n请选择..."))

          if choice ==1:
              userid = input("请输入账号...")
              userpwd = input("请输入密码...")

              userinfo = Userinfo.Userinfo(userid, userpwd)
              userinfoDao = UserinfoDao.UserinfoDao()
              if userinfoDao.userinfoRegister(userinfo):
                  print("数据已经成功录入系统...")

              else:
                  print("数据输入失败,请重新核对后再次输入...")
          elif choice==2:
               print("登录功能")
          elif choice == 3:
               break
          else:
               print("只能输入1,2,3三个选项,后续功能正在开发中...")

if __name__ == '__main__':
    main()



你可能感兴趣的:(mysq+ptthon,面向对象)