【原創】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()

  

相關文章
相關標籤/搜索