6 注册登录功能
注册登录功能
def register(cursor): # 获取用户名和密码 username = input('username>>>:').strip() password = input('password>>>:').strip() # 验证用户名是否已存在 sql = 'select * from userinfo where name=%s' cursor.execute(sql, (username,)) res = cursor.fetchall() if not res: sql1 = 'insert into userinfo(name,password) values(%s,%s)' cursor.execute(sql1, (username, password)) print('用户:%s注册成功' % username) else: print('用户名已存在') def login(cursor): # 获取用户名和密码 username = input('username>>>:').strip() password = input('password>>>:').strip() # 先获取是否存在用户名相关的数据 sql = 'select * from userinfo where name=%s' cursor.execute(sql, (username,)) res = cursor.fetchall() if res: # 校验密码 real_dict = res[0] if password == str(real_dict.get('password')): print('登录成功') else: print('密码错误') else: print('用户名不存在') def get_conn(): import pymysql # 创建链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='db_5', charset='utf8', autocommit=True # 涉及到增删改 自动二次确认 ) # 生成一个游标对象 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 让数据自动组织成字典 return cursor func_dic = {'1': register, '2': login} while True: print(""" 1.注册功能 2.登录功能 """) cursor = get_conn() choice = input('请输入功能编号>>>:').strip() if choice in func_dic: func_name = func_dic.get(choice) func_name(cursor) else: print('暂时没有当前功能编号')