要求:实现模拟sql登录,增删改查功能
#_*_conding:utf-8_*_ import os #登录验证部分 status_dic = { 'username': None, #设置一个登录用户 'status': False, #用于后面验证是否登录,登录状态 } def wrapper(func): #登录验证的装饰器 def inner1(): if status_dic['status']: ret = func() return ret else: print("请先进行登录") if login(): ret = func() return ret return inner1 def login(*args): #登录模块 with open("login.txt", encoding="utf-8", mode="r") as f: login_list = f.readlines() if args: #判断用户是否登录 status_dic['username'] = args[0] status_dic['status'] = True return True else: count = 0 while count < 3: if status_dic['status'] == False: username = input("请输入登录用户名:").strip() for i in login_list: if username == eval(i)["name"]: #对用户名进行检查,看是否存在,不存在直接走for循环的else判断,让用户进行注册 password = input("请登录用户密码:").strip() if password == eval(i)["password"]: #判断密码输入是否正确 print("login successfully") status_dic['username'] = username status_dic['status'] = True return sql_main() else: print("您还有%s次机会" % int(2 - count)) #3次机会验证,都错误会返回登录首页 count += 1 break else: print("检查用户名未注册...") y = input("是否注册(yes/no):").strip() if y == "yes" or y =="y": return register() elif y == "no" or y == "n": return home_page() else: print("输入错误...") else: print("错误太多次啦...") def register(): #注册模块 tag = True f1 = open("login.txt", encoding="utf-8", mode="r") login_list = f1.readlines() print("账号注册".center(50,"#")) while tag: username = input("请输入您注册的用户名:").strip() for i in login_list: if username == eval(i)["name"]: print("用户名已经存在,请重新输入") f1.close() break else: password = input("请输入您注册的密码:").strip() f2 = open("login.txt",encoding="utf-8",mode="a") f2.write("{} ".format({"name": username,"password": password})) tag = False print("注册成功") f2.close() return login(username, password) def log_out(): print("%s用户已注销" % status_dic['username']) status_dic['username'] = None status_dic['status'] = False def Quit_exit(): print("退出程序...") return exit() def home_page(): dic = { 1: login, 2: register, 3: sql_main, 4: log_out, 5: Quit_exit } while True: print('1:登录用户 2:注册用户 3:执行sql 4:注销 5:退出程序') bianhao = input("请输入选择的序列号:").strip() if bianhao.isdigit(): bianhao = int(bianhao) if 0 < bianhao <= 8: dic[bianhao]() else: print("输入编号不在范围内") else: print("输入的序列号只能是数字") #sql主程序,只有登录成功后才可以执行sql语句 s = ''' 支持的sql语法 1:insert insert into user values 张三,20,13412345678,BOSS 2:delete delete from user where id =6 3:update update user set name = Tom where id > 5 4:select select * from user select name,age from user where age > 2 select * from user where job='IT' select * from user where phone like 133 ''' @wrapper def sql_main(): print('