当前位置:   article > 正文

Python学生信息管理系统详解(MySQL版)_用python mysql写学生管理系统

用python mysql写学生管理系统

前言

  • Hello 大家好我是小余,今天给大家带来一个算是我的第一个完整的程序。
  • 友情提醒:在该代码的SQL语句中请用 %s 充当占位符,不要用 {} ,避免出错。
  • 还有一点,execute() 是执行SQL语句的,而commit() 是写入的。
  • 另外可能有时候游标会有报错,在本程序里我创建的是SScursor 流式游标,小余也对游标不熟,若代码运行出了问题,还请各位自行百度解决,也可以留言、私信。

数据库结构

在这里插入图片描述

数据源

21262174 余星舟 女 18 21大数据1班 大数据应用与技术 人工智能与大数据学院
21262175 张三 男 18 2021级软件2班 软件技术 人工智能与大数据学院
21262176 里斯 女 18 2021级软件3班 软件技术 人工智能与大数据学院
21262177 李四 男 19 2021级数据1班 大数据应用与技术 人工智能与大数据学院
21262178 王五 男 19 2021级软件3班 软件技术 人工智能与大数据学院
21262179 二麻 女 19 2021级软件3班 软件技术 人工智能与大数据学院
21262180 叶流沁 女 19 21软件3班 软件技术 信息工程学院

一、导入MySQL模块,并建立MySQL链接。

  • 如果有小伙伴导入MySQL报错的话,请参照小余的置顶博客哦(有一篇专门讲了module安装)
    在这里插入图片描述
  • 图中就是我们需要链接MySQL的步骤,至于connect的时候建议大家还是按照关键字(代码中红色字字体)来写入,不然可能无法识别

二、建立主程序

  • 和上一个文本版的一样建立一个主程序,只不过这次的主程序把原来的login 里面的验证身份加进来了。
    在这里插入图片描述

三、建立主菜单

在这里插入图片描述

  • 这个也是最基本的建设哦

四、开始编写程序

(一)、查询

1、查询菜单

在这里插入图片描述

  • 该菜单和上面的菜单差不多哦

2、按学号查询

在这里插入图片描述

  • 若我们不用for循环遍历输出:
  • 在这里插入图片描述
  • 下面是我们输出的结果
    在这里插入图片描述
  • 想必大家看到了吧,若不用for循环遍历输出的话,就会是这样,虽然说依旧输出了,但是不美观是吧。

3、按姓名查询

  • 我们添加一个按姓名查询来预防查询学号不成,但确实有这个人

在这里插入图片描述

  • 按姓名查询的格式和按学号一样,只不过数据不同。

(二)、删除记录

1、删除记录的菜单

  • 虽然我觉得这个菜单有点多余
    在这里插入图片描述

2、删除学生记录

在这里插入图片描述

(三)、增加记学生记录

在这里插入图片描述

  • 若是sqlite3那么就要用add.Rowcount来提取影响的行数

(四)、修改记录

在这里插入图片描述

总结

  • 总的来说MySQL版本比TXT版本好做的多,耗时少(当然那时候的txt是独立做出来的),对于这次的代码其实并不是最终的版本,只能说是能完成所有的需求,因为有一些是可以打包的代码并没有打包,因为对于定义函数之后括号内的内容调用有些不熟练,所以就没有再打包了,该版本也可以算是MySQL的成功版本了,大家好好观看吧。
  • 有什么建议大家就留言哦,不懂的,错了的,不管是注释还是代码都可以留言私信

源码

'''
function:StudentsInformationManageSystem
author:Sherry
Time:2021.12.26
'''
import pymysql

#定义数据库链接参数
host = '127.0.0.1'  #或者用local host
port = 3306
db = 'student'
user = 'root'
password = '111111'
conn = pymysql.connect(host=host, port=port, db=db, user=user, password=password)
def non():
    cursor = conn.cursor(pymysql.cursors.SSCursor)  #流式游标,默认返回元组
    return cursor

#插入学生记录
def add_student():
    cursor = non()
    id = int(input('学号:'))
    name = input('姓名:')
    genden = input('性别:')
    age = int(input('年龄:'))
    garden = str(input('班级:'))
    major = input('专业:')
    college = input('学院:')
    add = cursor.execute("insert into stu (id, name, genden, age, class, major, college)\
                   values(%s, %s, %s, %s, %s, %s, %s)", (id, name, genden, age, garden, major, college))
    if add == 1:
        conn.commit()
        print('插入成功!')
    else:
        print('插入失败!')

#按学号查询
def Q_by_id():
    cursor = non()
    choose_id = int(input('请输入学号:'))
    cursor.execute('select * from stu where id =%s', (choose_id))
    student = cursor.fetchall()
    for student in student:
        print('{}\t{}\t{}\t{}\t{}\t{}\t{}' .format(student[0], student[1], student[2], student[3], student[4], student[5], student[6]))
    re = input('\n是否继续查询y/n:')
    if re == 'y':
        Q_by_id()
    else:
        query_student()

#按姓名查询(以防学号输入错误)
def Q_by_name():
    cursor = non()
    choose_name = input('请输入姓名:')
    cursor.execute('select * from stu where name =%s', (choose_name))
    students = cursor.fetchall()
    for student in students:
        print('{}\t{}\t{}\t{}\t{}\t{}\t{}' .format(student[0], student[1], student[2], student[3], student[4], student[5], student[6]))
    re = input('\n是否继续查询y/n:')
    if re == 'y':
        Q_by_name()
    else:
        query_student()

#查询所有学生
def Q_all():
    cursor = non()
    cursor.execute('select * from stu')
    students = cursor.fetchall()
    for student in students:
        print('{}\t{}\t{}\t{}\t{}\t{}\t{}' .format(student[0], student[1], student[2], student[3], student[4], student[5], student[6]))

#查询的菜单
def query_student():
    while True:
        print('查询学生记录')
        print('================')
        print('1、按学号查询学生记录')
        print('2、按姓名查询学生记录')
        print('3、查询全部学生记录')
        print('4、返回上级菜单')
        print('=================')
        mc3 = int(input('请输入菜单号:'))
        if mc3 == 1:
            Q_by_id()
        elif mc3 == 2:
            Q_by_name()
        elif mc3 == 3:
            Q_all()
        else:
            break

#删除学生记录
def D_student():
    cursor = non()
    id = int(input('输入想要删除学生的学号:'))
    affect = cursor.execute('delete from stu where id = %s', (id))
    if affect == 1:
        conn.commit()
        print('删除成功!')
    else:
        print('删除失败!')

#删除的菜单
def delete_student():
    print('============================')
    print('1、删除学生信息')
    print('2、回到上一级菜单')
    print('============================')
    mc4 = int(input('Input menu number:'))
    if mc4 == 1:
        D_student()
    elif mc4 == 2:
        login()

#修改字段值
def modify_student():
        cursor = non()
        ID = int(input('请输入想要修改学生的学号:'))
        cursor.execute('select * from stu where id = %s', (ID))
        if cursor.fetchall() == []:
            print('未查找到学号是{}的学生'.format(ID))
            mc3 = input('是否重新查询?y/n')
            if mc3 == 'y':
                modify_student()
            else:
                login()
        else:
            print('==============')
            print('1、修改姓名')
            print('2、修改性别')
            print('3、修改年龄')
            print('4、修改班级')
            print('5、修改专业')
            print('6、修改学院')
            print('7、返回上级菜单')
            print('==============')
            mc2 = int(input('请输入菜单号:'))
            if mc2 == 1:
                name = input('请输入修改后的值:')
                a = cursor.execute('update stu set name = %s where id = %s', (name, ID))
                if a > 1:
                    conn.commit()
                    print('修改成功!')
                else:
                    print('修改失败!')
            elif mc2 == 2:
                sex = input('请输入修改后的值:')
                a = cursor.execute('update stu set genden = %s where id = %s', (sex, ID))
                if a > 1:
                    conn.commit()
                    print('修改成功!')
                else:
                    print('修改失败!')
            elif mc2 == 3:
                age = int(input('请输入修改后的值:'))
                a = cursor.execute('update stu set age = %s where id = %s', (age, ID))
                if a > 1:
                    conn.commit()
                    print('修改成功!')
                else:
                    print('修改失败!')
            elif mc2 == 4:
                garden = input('请输入修改后的值:')
                a = cursor.execute('update stu set class = %s where id = %s', (garden, ID))
                if a > 1:
                    conn.commit()
                    print('修改成功!')
                else:
                    print('修改失败!')
            elif mc2 == 5:
                major = input('请输入修改后的值:')
                a = cursor.execute('update stu set major = %s where id = %s', (major, ID))
                if a > 1:
                    conn.commit()
                    print('修改成功!')
                else:
                    print('修改失败!')
            elif mc2 == 6:
                college = input('请输入修改后的值:')
                a = cursor.execute('update stu set college = %s where id = %s', (college, ID))
                if a > 1:
                    conn.commit()
                    print('修改成功!')
                else:
                    print('修改失败!')
            else:
                login()

#登入后的菜单----展开
def login():
    while True:
        print('学生管理系统')
        print('================')
        print('1、增加学生记录')
        print('2、查询学生记录')
        print('3、修改学生记录')
        print('4、删除学生记录')
        print('5、返回上级菜单')
        print('=================')
        mc2 = int(input('输入菜单号:'))
        if mc2 == 1:
            add_student()
        elif mc2 == 2:
            query_student()
        elif mc2 == 3:
            modify_student()
        elif mc2 == 4:
            delete_student()
        else:
            break

#主程序
while True:
    print('登录系统')
    print('=========')
    print('1、登录')
    print('2、退出')
    print('=========')
    mc1 = int(input('输入菜单号:'))
    if mc1 == 1:
        username = input('请输入用户名:')
        password = input('请输入密码:')
        if username == 'admin' and password == '111111':
            login()
        else:
            print('账号或密码错误!')
    elif mc1 == 2:
        print('感谢使用!')
        break
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/806609
推荐阅读
相关标签
  

闽ICP备14008679号