This article example shares the specific code of python student information management system for your reference, the details are as follows
#!/usr/bin/env python # @Time : 2018/3/30 17:37 # @Author : KiritoLiu # @Contact : kiritoliuyhsky@ # @Site : # @File : Student Information Management System.py # @Software: PyCharm import pymysql import datetime import re def CalAge(Date): # Birthdays (year, month, day (in the database)) converted to age if Date == "NULL": return "None." try: Date = ('-') Birth = (int(Date[0]), int(Date[1]), int(Date[2])) Today = () if ( > ): NextYear = ( + 1, , ) elif ( < ): NextYear = (, + ( - ), ) elif ( == ): if ( > ): NextYear = ( + 1, , ) elif ( < ): NextYear = (, , + ( - )) elif ( == ): NextYear = 0 Age = - if NextYear == 0: # If today is the birthday return "%d" % (Age) else: DaysLeft = NextYear - Today return "%d" % (Age) except: return "Error." def seesql(): #View student table database db = (host="localhost", user="root", password="123456", db="stu", charset="utf8") # Create cursor objects cursor = () sql = "select ,,,,, from stu s order by sno" # View student list sorted by sno (student number) try: m = (sql) alist = () print("{:>3}|\t{:<4}\t|{}|\t{:<3}\t\t| {:<8}|{}| {}".format("School number.", "Name", "Gender", "Class.", "Telephone.", "Age", "Date of birth")) for vo in alist: birth = vo[5] bir = ("%Y-%m-%d") if bir == "1949-10-01": bir = "NULL" print("{:>5}|\t{:<4}\t| {} |\t{:<10}\t|{:<11}| {} | {}".format(vo[0], vo[1], vo[2], vo[3], vo[4], CalAge(bir), bir)) () except Exception as err: () print("SQL view failed! Error:", err) () def seeone(a): #Viewing a particular piece of data based on a student number db = (host="localhost", user="root", password="123456", db="stu", charset="utf8") # Create cursor objects cursor = () stuid =int(a) sql = "select ,,,,, from stu s where = '%d'" % stuid try: m = (sql) b = () if b == None: print("You have entered incorrectly and will exit the system.") quit() else: print("{:>3}|\t{:<4}\t|{}|\t{:<3}\t\t| {:<8}|{}| {}".format("School number.", "Name", "Gender", "Class.", "Telephone.", "Age", "Date of birth")) birth = b[5] bir = ("%Y-%m-%d") if bir == "1949-10-01": bir = "NULL" print("{:>5}|\t{:<4}\t| {} |\t{:<10}\t|{:<11}| {:<2} | {}".format(b[0], b[1], b[2], b[3], b[4], CalAge(bir), bir)) () except Exception as err: () print("SQL query failed! Error:", err) () def addmql(): #Add a piece of data db = (host="localhost", user="root", password="123456", db="stu", charset="utf8") # Create cursor objects cursor = () sql = "select from stu s" (sql) alist = () # Read all the student numbers in the database here blist = () #Create an empty tuple for storing school numbers print("The following school numbers are occupied and unavailable:") for i in alist: blist += i #Store all the student numbers print(i[0], end=" ") # Outputs the school number that has been used print() sno = int(input("Please enter the school number of the added trainee:\n")) if sno in blist: # Determine if the student number has been used, the student number cannot be duplicated print("The school number you entered has been taken! The system is about to exit!") quit() sname = input("Please enter the name of the added trainee:\n") sex = input("Please enter the gender of the trainee you are adding(maleorwomen):\n") if sex == "Male." or sex == "Female.": sex = sex else: sex = "Male." print("Sex entered incorrectly, defaulted to male.") cla = input("Please enter the class of the added student(precedent:Python01):\n") tel = input("Please enter the phone number of the added trainee:\n") if tel == (r"(1[3456789]\d{9})", tel): tel = tel print("The phone number was entered incorrectly and has been reset to null.") else: tel = "" sbir = input("Please enter the date of birth of the added trainee(precedent:2001-1-1):\n") if sbir == (r"(\d{4}-\d{1,2}-\d{1,2})", sbir): sbir = sbir else: sbir = "1949-10-01" print("The date of birth was entered incorrectly and has been reset to its initial value.") sql = "Insert into stu(sno,name,sex,cla,tel,birthday) values('%d', '%s', '%s', '%s', '%s', '%s')"%(sno, sname, sex, cla, tel, sbir) try: m = (sql) # Transaction submission () print("Number of articles successfully added:", m) print("The information you added is:") seeone(sno) except Exception as err: () print("SQL add failed! Error:", err) () def updatasql(): #Update to modify a piece of data db = (host="localhost", user="root", password="123456", db="stu", charset="utf8") # Create cursor objects cursor = () stuid = int(input("Please enter the student number of the trainee to be modified:\n")) # A class of no more than 100 students, indexed by stuid try: seeone(stuid) print("======The name of the student information that can be modified======") print("{0:2}{1:5}{2:5}{3:5}".format(" ", "1. Name", "2. Gender", "3. Classes")) print("{0:2}{1:5}{2}".format(" ", "4. Telephone", "5. Date of birth")) a = int(input("Please select the name of the student information to be modified(Student number cannot be changed):\n")) if a == 1: xm = input("Please enter the modified name: \n") sql = "UPDATE stu s SET = '%s' WHERE = '%d'" % (xm, stuid) elif a == 2: xb = input("Please enter the modified gender(maleorwomen):\n") if xb == "Male." or xb == "Female.": xb = xb else: xb = "Male." print("Sex entered incorrectly, defaulted to male.") sql = "UPDATE stu s SET = '%s' WHERE = '%d'" % (xb, stuid) elif a == 3: bj = input("Please enter the modified class:\n") sql = "UPDATE stu s SET = '%s' WHERE = '%d'" % (bj, stuid) elif a == 4: dh = input("Please enter the modified phone number:\n") sql = "UPDATE stu s SET = '%s' WHERE = '%d'" % (dh, stuid) if dh == (r"(1[3456789]\d{9})", dh): '''Regular expression matching to determine if input is qualified''' dh = dh else: dh = "" print("The phone number was entered incorrectly and has been reset to null.") elif a == 5: birday = input("Please enter the revised date of birth(specification:2000-1-1):") if birday == (r"(\d{4}-\d{1,2}-\d{1,2})", birday): '''Regular expression matching to determine if input is qualified''' birday = birday else: birday = "1949-10-01" print("The date of birth was entered incorrectly and has been reset to its initial value.") sql = "UPDATE stu s SET = '%s' WHERE = '%d'" % (birday, stuid) else: print("You have entered incorrectly and will exit!") # Exit here to prevent database data leakage due to some misuse. quit() (sql) () print("The revised information for this participant is:") seeone(stuid) except Exception as err: () print("SQL modification failed! Error:", err) () def delsql(): # Deleting a student's data db = (host="localhost", user="root", password="123456", db="stu", charset="utf8") # Create cursor objects cursor = () stuid = int(input("Please enter the student number of the student to be deleted:\n")) # A class of no more than 100 students, indexed by stuid try: print("======Name of the trainee information to be deleted======") seeone(stuid) a = input("Please confirm whether to delete the student information(y/n):\n") if a == 'y' or a == 'Y': sql = "delete from stu where sno = '%d'"%(stuid) (sql) else: print("Cancel trainee information deletion, exiting system soon.") quit() () print("This trainee's information has been deleted.") except Exception as err: () print("SQL delete failed! Error:", err) () def mainstu(): while True: # Output initial interface print("=" * 12, "Learner Information Management System", "=" * 15) print("{0:2}{1:13}{2:15}".format(" ", "1. View trainee information", "2. Add trainee information")) print("{0:2}{1:13}{2:15}".format(" ", "3. Modification of trainee information", "4. Deletion of trainee information")) print("{0:2}{1:13}".format(" ", "5. Exit the system")) print("=" * 45) key = int(input("Please enter the corresponding selection:\n")) # Determine and act on keypad values if key == 1: print("=" * 12, "Learner Information Viewing", "=" * 15) seesql() input("Press enter to continue.") elif key == 2: print("=" * 12, "Adding Student Information", "=" * 15) addmql() input("Press enter to continue.") elif key == 3: print("=" * 12, "Modification of student information", "=" * 15) seesql() updatasql() input("Press enter to continue.") elif key == 4: print("=" * 12, "Deletion of student information", "=" * 15) seesql() delsql() input("Press enter to continue.") elif key == 5: print("=" * 12, "see you again later", "=" * 12) quit() else: print("=" * 12, "Your input is incorrect,Please re-enter", "=" * 12) mainstu()
Companion database file with included data
-- MySQL dump 10.13 Distrib 5.7.12, for Win64 (x86_64) -- -- Host: localhost Database: stu -- ------------------------------------------------------ -- Server version 5.7.17-log /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `stu` -- DROP TABLE IF EXISTS `stu`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `stu` ( `id` int(3) NOT NULL AUTO_INCREMENT, `sno` int(3) NOT NULL, `name` varchar(20) NOT NULL, `sex` varchar(1) NOT NULL, `cla` varchar(10) NOT NULL, `tel` varchar(11) DEFAULT NULL, `birthday` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `stu_no_UNIQUE` (`sno`) ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `stu` -- LOCK TABLES `stu` WRITE; /*!40000 ALTER TABLE `stu` DISABLE KEYS */; INSERT INTO `stu` VALUES (1,1,'Zhang San','Male','Python01','12345678910','1999-01-01 00:00:00'),(2,2,'Li Si','Male','Python01','18866668888','1996-12-06 00:00:00'),(3,3,'Wang Wu','Male','Python02','12345665410','1996-11-27 00:00:00'),(4,4,'Zhao Liu','Female','Python02','12332233210','1997-10-24 00:00:00'),(5,5,'qq01','Female','Python03','13322223322','1990-01-31 00:00:00'),(6,6,'qq02','Male','Python03','12288886666','1992-02-20 00:00:00'),(7,7,'qq03','Female','Python03','13579264801','2000-10-30 00:00:00'),(8,8,'uu01','Male','Python01','18898084886','1998-08-08 00:00:00'),(9,9,'uu02','Female','Python02','12022000022','1994-04-01 00:00:00'),(10,10,'aa','Female','Python02','18899998888','2004-04-04 00:00:00'),(11,11,'bb','Male','Python03','19264664234','1995-05-15 00:00:00'),(25,12,'uu10','Male','Python04','17788992332','1996-12-06 00:00:00'),(28,13,'uu10','Female','Python04','13571854999','1996-12-06 00:00:00'); /*!40000 ALTER TABLE `stu` ENABLE KEYS */; UNLOCK TABLES; -- -- Dumping events for database 'stu' -- -- -- Dumping routines for database 'stu' -- /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2018-03-31 15:10:58
This is the whole content of this article.