【原创】Python3 连接MySQL数据库,实现注册、登录、成绩录入与查看功能
import pymysql class Student: def register(self): print("=============注册页面=============") n = 3 user = input("请输入账号:").strip() while n > 0: n = n - 1 pass_wd = input("请输入密码:").strip() pass_wd_sec = input("确认你的密码:").strip() sql_1 = "insert into student_info(user_name,passwd) values(%s,%s)" if pass_wd != pass_wd_sec: print("两次输入不一致,请重新输入!\n") print("你还有" + str(n) + "次机会!") continue else: res_1 = [user, pass_wd_sec] cur.execute(sql_1, res_1) conn.commit() print("注册成功!\n") break def login(self): n = 3 sql = "select passwd from student_info where user_name =%s" res = [user] cur.execute(sql, res) psw = cur.fetchall() while n > 0: n = n - 1 pass_wd = input("请输入你的密码:").strip() if pass_wd != psw[0][0]: print("密码错误,请重新输入!\n") print("你还有" + str(n) + "次机会!") continue else: print("登录成功!\n") break def achievement(self): # 这里实现成绩录入与成绩查询 print("=================成绩录入与查询=====================") status = input("选择你的操作 【0:录入,1:查看,其他:退出】") while True: if status == '0': student_no = input("学号:") subject_name = input("课程名称:") score = int(input("课程分数")) rest_insert = [student_no, subject_name, score] cur.execute(sql_insert, rest_insert) conn.commit() break elif status == '1': student_no = input("学号:") rest_choice = [student_no] cur.execute(sql_choice, rest_choice) rest_cho = cur.fetchall() for sc in rest_cho: print(sc) conn.commit() break else: break def course_selection(self): print("\n===================选课模块===============") student_no = input("输入学号:") while True: alternative = input("请操作【1|查看我的课程 2|查看当前所有可选课程 3|选课 other|退出】:") if alternative == "1": my_res = [student_no] cur.execute(my_sql, my_res) my_subject = cur.fetchall() conn.commit() for i in my_subject: print(str(i)) print("以上是你当前所有的课程!\n") elif alternative == "2": cur.execute(all_sql) all_subject = cur.fetchall() conn.commit() for i in all_subject: print(str(i)) print("以上是你当前可选的课程有\n") elif alternative == "3": subject_name = input("请输入你要选择的课程名称:\n") subject_id = input("请输入你要选择的课程id:\n") all_to_my_res = [student_no, subject_id, subject_name] cur.execute(all_to_my, all_to_my_res) conn.commit() print("本次选课结果为" + str(all_to_my_res) + "\n") else: print("欢迎下次使用!") break if __name__ == '__main__': user = input("请输入账号:").strip() conn = pymysql.connect( host="localhost", port=3306, user="root", passwd="mysql", db="student", charset="utf8") cur = conn.cursor() sql = "select user_name from student_info where user_name =%s" my_sql = "select subject_id, subject_name from my_subject where student_no = %s;" sql_insert = "insert into student_score(student_no,subject_name,score) values(%s,%s,%s)" sql_choice = "select * from student_score where student_no = %s" all_sql = "select * from subject_choice;" all_to_my = "insert into my_subject(student_no, subject_id, subject_name) VALUES (%s,%s,%s)" res = [user] cur.execute(sql, res) name = cur.fetchall() s = Student() if name == (): print("账号不存在,请返回注册\n") s.register() print("=============登录界面=============") user = input("请输入账号:").strip() s.login() s.achievement() else: s.login() s.achievement() s.course_selection() cur.close() conn.close()