纯python+mysql实现字符界面学生管理系统练习

发布于 2024-01-05  129 次阅读


之前写过一个基于纯python的学生管理系统,当时加了dos命令行的优化,还挺好的,但是参赛没拿奖,后来发现一等奖被内定(没我写的好),这件事对我打击挺大,最近又捡起python,不聊这些咱们直接聊技术

首先我使用连接sql的支持库是PyMySQL可以自行百度安装配置。

import pymysql
conn = pymysql.connect(
    host='*****',
    user='*****',
    password='*****', 
    db='*****', charset='utf8')
cur = conn.cursor()

这是inc文件,因为数据库用的是我远程服务器的,所以被我删了.

import pymysql
from Config.inc import cur
from Config.inc import conn


def StudentIndex():
    sql="select * from StudentData"
    cur.execute(sql)
    data = cur.fetchall()
    for row in data:
        print(row)

def StudentText():
    StuID = input("请输入一个学生的学号:")
    sql = f''' select * from StudentData where ID={StuID} '''
    cur.execute(sql)
    data = cur.fetchall()
    print(data)

def StudentInsert():
    SexStep=1
    name = input("请输入姓名:")
    StuID = input("请输入是为学号:")
    Age = int(input("请输入年龄:"))

    while SexStep==1:
        SexID = input("请输入年龄(男|女):")
        if SexID.strip() == "男":
            Sex=1
            SexStep=0
        elif SexID.strip() == "女":
            Sex=0
            SexStep=0
        else:
            print("请重新输入性别")

    sql=f'''insert into StudentData values('{name}','{StuID}',{Age},{Sex})'''
    cur.execute(sql)
    conn.commit()

def StudentDelete():
    StuID=input("请输入一个学生的学号:")
    name = NameFinder(StuID)
    if len(name) == 0:
        print("未能查到该学号的学生信息",len(name))
    else:
        NameStep = input(f'''您要删除{name}的数据吗(y/n):''')

    if NameStep.upper().strip() == "Y":
        sql = f'''delete from StudentData where ID='{StuID}' '''
        cur.execute(sql)
        conn.commit()

def StudentUpdate():
    StuID = input("请输入一个学生的学号:")
    name = NameFinder(StuID)
    if len(name) == 0:
        print("未能查到该学号的学生信息", len(name))
    else:
        NameStep = input(f'''您要修改{name}的数据吗(y/n):''')

    if NameStep.upper().strip() == "Y":
        chose = int(input("您要进行全部修改(1)还是单个修改(2)"))

        if chose == 1 :
            sql = f''' update StudentData set Name = '{name}',ID = '{StuID}',Age = '{Age}',Sex = '{Sex}' where ID = '{StuID}' '''

        elif chose == 2 :
            onechose = int(input("您要进行什么修改? 姓名(1)学号(2)年龄(3)性别(4)"))
            if onechose == 1:
                sql = f'''update StudentData set Name='{name}' where Name = '{name}' '''
            elif onechose == 2:
                sql = f'''update StudentData set ID='{StuID}' where Name = '{name}' '''
            elif onechose == 3:
                sql = f'''update StudentData set Age='{Age}' where Name = '{name}' '''
            elif onechose == 4:
                sql = f'''update StudentData set Sex='{Sex}' where Name = '{name}' '''
            else:
                print("没有此功能")
        else:
            print("没有此功能")
    else:
        chose=0

    if chose!=0:
        cur.execute(sql)
        conn.commit()


def NameFinder(StuID):
    sql = f''' select Name from StudentData where ID='{StuID}' '''
    cur.execute(sql)
    name = cur.fetchall()
    return name


def main():
    x=0
    while x != 10:
        print("1.学生信息全部显示:")
        print("2.学生信息查询:")
        print("3.学生信息添加")
        print("4.学生信息删除")
        print("5.学生信息修改")
        print("6.退出此程序")
        x=int(input("请输选择功能"))
        if x == 1:
            StudentIndex()
        elif x == 2:
            StudentText()
        elif x == 3:
            StudentInsert()
        elif x == 4:
            StudentDelete()
        elif x == 5:
            StudentUpdate()
        elif x == 6:
            break

main()

因为是练习没写备注,而且就100多行就别写备注了,说白了没想用,就权当我个人的康复训练了。

太阳马上落山,你们的孩子居然不害怕
最后更新于 2024-01-05