美文网首页python我爱编程
python——封装sql,登陆密码验证

python——封装sql,登陆密码验证

作者: Jalynn葸 | 来源:发表于2018-06-11 17:22 被阅读13次

    MysqlHelper.py文件

    import pymysql
    class MysqlHelper:
        def __init__(self, host, port, db, user, passwd, charset='utf8'):
            self.host = host
            self.port = port
            self.db = db
            self.user = user
            self.passwd = passwd
            self.charset = charset
    
        def open(self):
            self.conn = pymysql.connect(host=self.host, port=self.port, db=self.db, user=self.user, passwd=self.passwd, charset=self.charset)
            self.cursor = self.conn.cursor()
    
        def close(self):
            self.cursor.close()
            self.conn.close()
    
        def cud(self, sql, params):
            try:
                self.open()
                self.cursor.execute(sql, params)
                self.conn.commit()
                self.close()
                print('ok')
            except Exception as e:
                print("错误信息:%s"%e)
    
        def all(self, sql, params):
            try:
                self.open()
                self.cursor.excute(sql, params)
                result = self.cursor.fetchall()
                self.close()
                return result
            except Exception as e:
                print(e)
    
    from MysqlHelper import MysqlHelper
    #修改
    name = input("请输入姓名")
    id1 = input("输入考生编号")
    sql = 'update students set name=%s where id=%s'
    params=[name,id1]
    sqlhelper = MysqlHelper('localhost', 3306, 'epoque', 'root', 'xjx')
    sqlhelper.cud(sql, params)
    
    from MysqlHelper import MysqlHelper
    from hashlib import sha1
    
    name = input("请输入用户名:")
    pwd = input("请输入密码:")
    s1 = sha1()
    s1.update(pwd.encode("utf-8"))
    pwd2 = s1.hexdigest()
    
    print("_______________")
    print(pwd2)
    #根据用户名查询密码
    
    sql = 'select passwd from users where name=%s'
    helper = MysqlHelper('localhost', 3306, 'epoque', 'root', 'xjx')
    result = helper.all(sql, [name])
    if len(result)==0:
        print('用户名错误')
    elif result[0][0]== pwd2:
        print("登陆成功")
    else:
        print("密码错误")
    print(result)
    

    相关文章

      网友评论

        本文标题:python——封装sql,登陆密码验证

        本文链接:https://www.haomeiwen.com/subject/xdmheftx.html