sql中datetime类型数据怎么输入_Python模拟登录成功与失败处理方式(链接数据库)...

70e3250d1974e9b36ce3f7a44636e2fc.png

说明任务点:

1)用户输入用户名,如果没有,用户不能登录;

2)如果用户连续三次登录密码之间,用户会被锁定一段时间;

3)在用户被锁定一段时间后,可以尝试再次登录;

程序使用库:

datetime https://docs.python.org/2/library/datetime.html

pymysql http://pymysql.readthedocs.io/en/latest/index.html

数据库设计如下:

4a2b2fe426db683e8feaa7ddfae5d6d9.png

数据库数据形式如下

cf719ad8498cfff181ecf87bd39c6e6d.png

程序说明:

程序结构为顺序结构,未涉及到函数的调用之类,只是为了练习使用pymysql 进行增删该查的功能

整体思路如下:

(1) 链接数据库

#创建数据库的链接
connection = pymysql.connect(host='127.0.0.1',
                             port=3306,
                             user='root',
                             password='123456',
                             db='myschool',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)

(2) 交互输入登陆用名名,并根据输入查询数据库数据

userName = input("输入用户名:")
        if "q"!=userName and "Q"!=userName:
                with connection.cursor() as cursor:
                    #根据用户名查出用户信息
                    sql = 'select * from t_user where name = %s'
                    result =  cursor.execute(sql,userName);

(3) 判断输入的用户名是否存在,如不存在则返回重新输入,如存在则判断是否被锁定,如果被锁定还得判断是否已经过了锁定时间

# 判断是否存在此用户
                    if 0==result:
                        print("无此用户名存在!")
                        continue
                    #获取用户信息
                    item = cursor.fetchone()
                    is_login = False
                    # 判断用户是否被禁
                    if item["is_based"]:
                        last_login_time = item["login_time"]
                        login_time = datetime.datetime.now()
                        waiting_time = int(((login_time-last_login_time).total_seconds())/60)
                        #用户如果被禁,判断还需要多长时间等待
                        if (waiting_time-3)<0:
                            print("账号暂时锁定,请等待%d分钟"%(3-waiting_time))
                            break

(4) 连续输入三次,如果密码输入错则被登录失败,否则登陆成功

times = 0
                    # 3次输入密码的机会
                    while times<3:
                        password = input("请输入密码:")
                        if password != item["password"]:
                            times += 1
                        else:
                            is_login = True
                            break

                    # 登录成功与否,禁用信息以及登录信息数据都在数据库进行更新
                    if is_login:
                        is_based = 0
                        # 将datetime转换字符串类型
                        login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                        print("欢迎%s,登陆成功!"%(userName))
                    else:
                        is_based = 1
                        login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                        print("登录失败,请等待10min")

(4) 更新数据库的信息

# 更新数据库的Mysql语句
                    sql = "update t_user set is_based = %d , login_time = '%s' where name='%s'"%(is_based,login_time,userName)
                    cursor.execute(sql)

                    # 由于对数据库进行了更新,故需要提交事务
                    connection.commit()

全部代码如下:

import pymysql
import datetime
'''
遇到不懂的问题?Python学习交流群:766545907满足你的需求,资料都已经上传群文件,可以自行下载!
'''
#创建数据库的链接
connection = pymysql.connect(host='127.0.0.1',
                             port=3306,
                             user='root',
                             password='123456',
                             db='myschool',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)
try:
    while True:
        userName = input("输入用户名:")
        if "q"!=userName and "Q"!=userName:
                with connection.cursor() as cursor:
                    #根据用户名查出用户信息
                    sql = 'select * from t_user where name = %s'
                    result =  cursor.execute(sql,userName);
                    # 判断是否存在此用户
                    if 0==result:
                        print("无此用户名存在!")
                        continue
                    #获取用户信息
                    item = cursor.fetchone()
                    is_login = False
                    # 判断用户是否被禁
                    if item["is_based"]:
                        last_login_time = item["login_time"]
                        login_time = datetime.datetime.now()
                        waiting_time = int(((login_time-last_login_time).total_seconds())/60)
                        #用户如果被禁,判断还需要多长时间等待
                        if (waiting_time-3)<0:
                            print("账号暂时锁定,请等待%d分钟"%(3-waiting_time))
                            break

                    times = 0
                    # 3次输入密码的机会
                    while times<3:
                        password = input("请输入密码:")
                        if password != item["password"]:
                            times += 1
                        else:
                            is_login = True
                            break

                    # 登录成功与否,禁用信息以及登录信息数据都在数据库进行更新
                    if is_login:
                        is_based = 0
                        # 将datetime转换字符串类型
                        login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                        print("欢迎%s,登陆成功!"%(userName))
                    else:
                        is_based = 1
                        login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                        print("登录失败,请等待10min")

                    # 更新数据库的Mysql语句
                    sql = "update t_user set is_based = %d , login_time = '%s' where name='%s'"%(is_based,login_time,userName)
                    cursor.execute(sql)

                    # 由于对数据库进行了更新,故需要提交事务
                    connection.commit()
                    break
        else:
            print("退出!!!")
            break
finally:
    # 最终关闭链接
    connection.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值