溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Python中使用mysql實現一個學生管理系統

發布時間:2021-04-20 17:12:49 來源:億速云 閱讀:386 作者:Leah 欄目:開發技術

怎么在Python中使用mysql實現一個學生管理系統?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

python是什么意思

Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向對象的腳本語言,其最初的設計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發獨立的項目和大型項目。

具體如下:

import pymysql
import re
 
def idinput(string):
 ID = input(string)
 pattern = re.compile("^\d{1,3}$")
 while not re.match(pattern, ID):
  ID = input("請輸入1-3位整數:")
 return ID
 
def appendStudentInfo():
 ID =idinput("請輸入學生學號:")
 db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
 cursor=db.cursor()
 sql = "select * from StuSys where ID = '%s'" % ID
 cursor.execute(sql)
 while cursor.rowcount > 0 :
  ID = idinput("該學號已存在,請重新輸入:")
  sql = "select * from StuSys where ID = '%d'" % int(ID)
  cursor.execute(sql)
 
 name=input("請輸入學生姓名:")
 
 chinese=input("請輸入語文成績:")
 while not chinese.isdigit() or int(chinese)>100 or int(chinese)<0:
  chinese = input("輸入錯誤,請重新輸入:")
 
 math =input("請輸入數學成績:")
 while not math.isdigit() or int(math) > 100 or int(math) < 0:
  math = input("輸入錯誤,請重新輸入:")
 
 english=input("請輸入英語成績:")
 while not english.isdigit() or int(english) > 100 or int(english) < 0:
  english = input("輸入錯誤,請重新輸入:")
 
 total=int(chinese)+int(math)+int(english)
 
 sql="""INSERT INTO StuSys(ID,
   NAME,CHINESE,ENGLISH,MATH,TOTAL)
   VALUES (%s,%s,%s,%s,%s,%s)"""
 cursor.execute(sql,(ID,name,chinese,english,math,total))
 db.commit()
 db.close()
 
def delstudent():
 delstudentid = idinput("請輸入要刪除的學生學號:")
 if querystudent(delstudentid):
  select = input("是否刪除:是(Y)/否(N)")
  if select == "Y" or select == "y":
   db = pymysql.connect(host="127.0.0.1", user="root", passwd="hisense", db="test", port=3306, charset="utf8")
   cursor = db.cursor()
   sql = "delete from stusys where ID =%s" %delstudentid
   cursor.execute(sql)
   db.commit()
   db.close()
   print("刪除成功")
  elif select == "N" or select == "n":
   print("取消刪除")
  else:
   print("輸入錯誤")
 
 
def querystudent(querystudentid):
 db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
 cursor=db.cursor()
 sql="select * from stusys where ID=%s"%querystudentid
 cursor.execute(sql)
 if cursor.rowcount ==0 :
  print("不存在該學生信息")
  return False
 else:
  print("該學生信息如下:")
  results =cursor.fetchall()
  print("ID=%d,NAME=%s,CHINESE=%d,ENGLISH=%d,MATH=%d,TOTAL=%d" % \
   (results[0][0], results[0][1], results[0][2], results[0][3], results[0][4],results[0][5]))
  return True
 
def modifystudentifo():
 modifyid = idinput("請輸入要的學生學號:")
 if querystudent(modifyid):
  name = input("請重新輸入學生姓名:")
 
  chinese = input("請重新輸入語文成績:")
  while not chinese.isdigit() or int(chinese) > 100 or int(chinese) < 0:
   chinese = input("輸入錯誤,請重新輸入:")
 
  math = input("請重新輸入數學成績:")
  while not math.isdigit() or int(math) > 100 or int(math) < 0:
   math = input("輸入錯誤,請重新輸入:")
 
  english = input("請重新輸入英語成績:")
  while not english.isdigit() or int(english) > 100 or int(english) < 0:
   english = input("輸入錯誤,請重新輸入:")
 
  total = int(chinese) + int(math) + int(english)
  db = pymysql.connect(host="127.0.0.1", user="root", passwd="hisense", db="test", port=3306, charset="utf8")
  cursor = db.cursor()
  sql1="update stusys set name ='%s' where id = %s"%(name,modifyid)
  cursor.execute(sql1)
  sql2="update stusys set math = %s where id = %s"%(math,modifyid)
  cursor.execute(sql2)
  sql3 = "update stusys set english = %s where id =%s"%(english,modifyid)
  cursor.execute(sql3)
  sql4 = "update stusys set total = %s where id = %s"%(total,modifyid)
  cursor.execute(sql4)
  sql5 = "update stusys set chinese = %s where id = %s"%(chinese,modifyid)
  cursor.execute(sql5)
  db.commit()
  db.close()
 
def allinfo():
 db=pymysql.connect(host="127.0.0.1",user="root",passwd="hisense",db="test",port=3306,charset="utf8")
 cursor=db.cursor()
 sql="select * from stusys"
 cursor.execute(sql)
 results= cursor.fetchall()
 for row in results:
  ID = row[0]
  NAME = row[1]
  CHINESE = row[2]
  ENGLISH = row[3]
  MATH = row[4]
  TOTAL = row[5]
  # 打印結果
  print("ID=%d,NAME=%s,CHINESE=%d,ENGLISH=%d,MATH=%d,TOTAL=%d" % \
    (ID, NAME, CHINESE, ENGLISH, MATH,TOTAL))
 
def studentMenu():
 print("="*30)
 print("學生管理系統")
 print("1、添加學生信息")
 print("2、刪除學生信息")
 print("3、查詢學生信息")
 print("4、修改學生信息")
 print("5、全部學生信息")
 print("6、退出")
 print("="*30)
 
 
 
if __name__ == '__main__':
 
 while True:
  studentMenu()
  menuindex = input("請輸入選項序號:")
  while not menuindex.isdigit():
   menuindex = input("輸入錯誤,請重新輸入:")
  if int(menuindex) ==1:
   appendStudentInfo()
  elif int(menuindex) ==2:
   delstudent()
  elif int(menuindex) ==3:
   querystudentid = idinput("請輸入要查詢的學生學號:")
   querystudent(querystudentid)
  elif int(menuindex) ==4:
    modifystudentifo()
  elif int(menuindex) == 5:
    allinfo()
  elif int(menuindex) == 6:
   break
  else:
   print("輸入序號無效")

關于怎么在Python中使用mysql實現一個學生管理系統問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女