Python项目:学生管理系统(学生的增删改查)

版权声明:本文为博主原创文章,版权归原作者小思所有,转载或者引用本文内容请注明来源及原作者,https://blog.csdn.net/zeal9s/ https://blog.csdn.net/zeal9s/article/details/83274119

工具: Python 3.7
pycharm
mysql
本项目是对学生的基本增删改查(控制台版)
项目功能演示:https://download.csdn.net/download/zeal9s/10737062
项目的基本要求:https://blog.csdn.net/zeal9s/article/details/83274119
项目基本结构图:
在这里插入图片描述
dao包中的BaseDao类:

# -*- encoding:utf-8 -*-
"""
@作者:小思
@文件名:BaseDao.py
@时间:2018/10/19  8:51
@文档说明:连接数据库和关闭数据库的方法
"""
import pymysql
import com.zs.entity.Student as student

s = student.Student()


# 1.查询所有学生信息
def getAll():
    connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
    cursor = connect.cursor()
    cursor.execute("select * from Student")
    data = cursor.fetchall()
    connect.close()
    return data


# 根据学生姓名查询学生信息
def getStudentBySname(s):
    connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
    cursor = connect.cursor()
    sql = "select *  from Student where sname='%s'" % (s.getSname())
    cursor.execute(sql)
    data = cursor.fetchall()
    connect.close()
    return data


# 根据sid查询单个学生信息
def getStudentBySid(s):
    connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
    cursor = connect.cursor()
    sql = "select *  from Student where sid=%d" % (s.getSid())
    cursor.execute(sql)
    data = cursor.fetchone()
    connect.close()
    return data


# 添加学生信息
def addStudent(s):
    connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
    cursor = connect.cursor()
    sql = "insert into Student values(null,'%s','%s','%d')" % (s.getSname(), s.getSsex(), s.getSage())
    cursor.execute(sql)
    connect.commit()
    connect.close()


# 删除学生信息
def delStudent(s):
    connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
    cursor = connect.cursor()
    sql = "delete from Student where sid=%d" % (s.getSid())
    cursor.execute(sql)
    connect.commit()
    connect.close()


# 修改学生信息
def updStudent(s):
    connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
    cursor = connect.cursor()
    sql = "update Student set sname='%s',ssex='%s',sage=%d where sid=%d" % (
    s.getSname(), s.getSsex(), s.getSage(), s.getSid())
    cursor.execute(sql)
    connect.commit()
    connect.close()

# 测试查询所有学生信息
# d=getAll()
# print(d)

# 测试查询单个学生信息
# stu = student.Student()
# stu.setSname("小明")
# sd = getStudentBySname(stu)
# print(sd)

# 测试添加学生信息
# s= student.Student()
# s.setSname("小明")
# s.setSsex("男")
# s.setSage(19)
# addStudent(s)

# 测试删除学生信息
# stu = student.Student()
# stu.setSid(9)
# delStudent(stu)

# 测试修改
# s= student.Student()
# s.setSid(15)
# s.setSname("小d")
# s.setSsex("男")
# s.setSage(19)
# updStudent(s)

entity包的Student类:

# -*- encoding:utf-8 -*-
"""
@作者:小思
@文件名:Student.py
@时间:2018/10/19  0:43
@文档说明:学生的实体类
"""


class Student:
    __sid = 0
    __sname = ""
    __ssex = ""
    __sage = 0

    def setSid(self, sid):
        self.__sid = sid

    def getSid(self):
        return self.__sid

    def setSname(self, sname):
        self.__sname = sname

    def getSname(self):
        return self.__sname

    def setSsex(self, ssex):
        self.__ssex = ssex

    def getSsex(self):
        return self.__ssex

    def setSage(self, sage):
        self.__sage = sage

    def getSage(self):
        return self.__sage



main包下的main类

# -*- encoding:utf-8 -*-
"""
@作者:小思
@文件名:main.py
@时间:2018/10/19  11:16
@文档说明:运行的主界面
"""

import com.zs.dao.BaseDao as bd
import com.zs.entity.Student as student
import re

# 欢迎的方法
def welcome():
    print("*****************************************欢迎使用《学生信息管理系统》******************************************")
    print("1.查询所有学生信息   2.查询单个学生信息   3.添加学生信息  4.删除学生信息   5.修改学生信息   6.退出系统")

def main():
    try:
        while True:
            welcome()
            # 接收用户填写的序号进行功能判断
            key = int(input("请输入操作编号:"))
            if key == 1:
                students1 = bd.getAll()
                print('*' * 40)
                print("学号       姓名         性别        年龄")
                print("-" * 40)
                for s1 in students1:
                    print(s1[0], '        ', s1[1], '        ', s1[2], '       ', s1[3])
                    print("-"*40)
                print('*' * 40)
            elif key == 2:
                sname2 = input("请输入要查询的学生姓名:")
                # 判断是否有该学生的信息
                contain = 0
                s2 = student.Student()
                s2.setSname(sname2)
                students2 = bd.getStudentBySname(s2)
                for ss2 in students2:
                    # 不能写ss2.getSname()
                    if ss2[1] == s2.getSname():
                        contain = 1
                if contain == 0:
                    print("查询失败,没有此学生的信息,请重新输入!")
                else:
                    print('*' * 40)
                    print("学号       姓名         性别        年龄")
                    print("-" * 40)
                    for f in students2:
                        print(int(f[0]), '        ', f[1], '        ', f[2], '       ', f[3])
                        print("-" * 40)
                    print('*' * 40)


            elif key == 3:
                sname3 = input("请输入学生姓名:")
                ssex3 = input("请输入学生性别:")
                sage3 = int(input("请输入学生年龄:"))
                print('*' * 40)
                print("姓名         性别        年龄")
                print("-" * 40)
                print(sname3, '        ',ssex3, '       ', sage3)
                print('*' * 40)
                isAdd = input("确认要将此信息添加吗?Y/N")
                if isAdd == 'Y':
                    s3 = student.Student()
                    s3.setSname(sname3)
                    s3.setSsex(ssex3)
                    s3.setSage(sage3)
                    bd.addStudent(s3)
                    print("添加成功")
                elif isAdd == 'N':
                    print("您已取消添加")
                else:
                    print("您输入有误,请重新输入")
                print('*' * 40)
                print("学号       姓名         性别        年龄")
                print("-" * 40)
                for st in bd.getAll():
                    print(st[0], '        ', st[1], '        ', st[2], '       ', st[3])
                    print("-" * 40)
                print('*' * 40)

            elif key == 4:
                sid = int(input("请输入要删除学生的编号:"))
                s4 = student.Student()
                s4.setSid(sid)
                # 查询是否有该学生
                r=bd.getStudentBySid(s4)
                if r==None:
                    print("没有查询到该学生的信息,无法进行删除!")
                else:
                    isTure = input("确认要删除该学生信息吗?Y/N")
                    students4 = bd.getAll()
                    if isTure == 'Y':
                        bd.delStudent(s4)
                        print("删除成功")
                        print('*' * 40)
                        print("学号       姓名         性别        年龄")
                        print("-" * 40)
                        for ss4 in bd.getAll():
                            print(ss4[0], '        ', ss4[1], '        ', ss4[2], '       ', ss4[3])
                            print("-" * 40)
                        print('*' * 40)
                    elif isTure == 'N':
                        print("您已取消删除")
                        print('*' * 40)
                        print("学号       姓名         性别        年龄")
                        print("-" * 40)
                        for ss4 in students4:
                            print(ss4[0], '        ', ss4[1], '        ', ss4[2], '       ', ss4[3])
                            print("-" * 40)
                        print('*' * 40)
                    else:
                        print("您输入有误,请重新输入")



            elif key == 5:
                sid=int(input("请输入要修改学生的编号:"))
                s5 = student.Student()
                s5.setSid(sid)
                # 查询是否有该学生
                t = bd.getStudentBySid(s5)
                if t == None:
                    print("没有查询到该学生的信息,无法进行修改!")
                else:
                    print('*' * 40)
                    print("学号       姓名         性别        年龄")
                    print("-" * 40)
                    print(t[0], '        ', t[1], '        ', t[2], '       ', t[3])
                    print('*' * 40)
                    sname5=input("名字修改后为:")
                    ssex5= input("性别修改后为:")
                    sage5=int(input("年龄修改后为:"))
                    s5.setSname(sname5)
                    s5.setSsex(ssex5)
                    s5.setSage(sage5)
                    bd.updStudent(s5)
                    print('*' * 40)
                    print("学号       姓名         性别        年龄")
                    print("-" * 40)
                    print(sid, '        ', sname5, '        ', ssex5, '       ', sage5)
                    print('*' * 40)
                    isTure=input("确认要修改吗?Y/N")
                    if isTure=="Y":
                        upd = bd.getStudentBySid(s5)
                        print("修改成功")
                        print('*' * 40)
                        print("学号       姓名         性别        年龄")
                        print("-" * 40)
                        for stu5 in bd.getAll():
                            print("-" * 40)
                            print(stu5[0], '        ', stu5[1], '        ', stu5[2], '       ', stu5[3])
                        print('*' * 40)
                    elif isTure=="N":
                        print("您已取消修改")
                        print('*' * 40)
                        print("学号       姓名         性别        年龄")
                        print("-" * 40)
                        for stu5 in bd.getAll():
                            print("-" * 40)
                            print(stu5[0], '        ', stu5[1], '        ', stu5[2], '       ', stu5[3])
                        print('*' * 40)
                    else:
                        print("您输入有误,请重新输入")

            elif key == 6:
                sel=input("您确定要离开我了吗?Y/N")
                if sel=="Y":
                    print("感谢您的使用!")
                    break
                elif sel=="N":
                    main()
                else:
                    print("您输入有误,请重新输入")

            else:
                print("没有此操作,请输入正确编号!")
    except ValueError:
        print("您的操作可能存在错误,原因如下:\n(1)请您输入操作之后再进行回车\n(2)您输入的值字段值不匹配,请重新输入!")
        main()

# 主页面调用主方法
main()

在主界面运行即可
项目运行界面图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

猜你喜欢

转载自blog.csdn.net/zeal9s/article/details/83274119