Python课程-w73班
Louyj's Blog
Toggle navigation
Python课程-w73班
Home
班级课件
课堂代码
课后作业
学习资料
Archives
Tags
2022-12-25课堂代码
2022-12-25 13:07:10
11
0
0
python-w73
``` # 定义一个列表来存储所有学生信息 import sqlite3 conn = sqlite3.connect('student.db') conn.execute(''' create table if not exists student ( sid text primary key not null, name text not null, sex text not null, phone text not null ) ''') # 打印功能菜单 def print_menu(): print('=' * 30) print('学生管理系统') print('1.添加学生信息') print('2.删除学生信息') print('3.修改学生信息') print('4.显示所有学生信息') print('0.退出系统') print('=' * 30) # 添加学生信息 def add_stu_info(): sid = input('请输入新学生的学号:') cursor = conn.execute("select * from student where sid=?", (sid,)) if len(cursor.fetchall()) > 0: print('要添加的学生已存在') else: name = input('请输入新学生的姓名:') sex = input('请输入新学生的性别:') phone = input('请输入新学生的手机号码:') conn.execute("insert into student(sid,name,sex,phone) values (?,?,?,?)", (sid, name, sex, phone)) conn.commit() # 删除学生信息 def del_stu_info(): sid = input('请输入要删除的学号:') cursor = conn.execute("select * from student where sid=?", (sid,)) if len(cursor.fetchall()) > 0: conn.execute("delete from student where sid=?", (sid,)) conn.commit() else: print('要删除的学号有误') # 修改学生信息 def rev_stu_info(): sid = input('请输入要修改学生的学号:') cursor = conn.execute("select * from student where sid=?", (sid,)) if len(cursor.fetchall()) <= 0: print("要修改的序号有误") else: rev_name = input('请输入要修改学生的姓名:') rev_sex = input('请输入要修改学生的性别:') rev_phone = input('请输入要修改学生的手机:') conn.execute("update student set name=?,sex=?,phone=? where sid=?", (rev_name, rev_sex, rev_phone, sid)) conn.commit() # 查找全部学生信息 def show_stu_info(): cursor = conn.execute('select sid,name,sex,phone from student order by sid') if len(cursor.fetchall()) > 0: cursor = conn.execute('select sid,name,sex,phone from student order by sid') print('学生的信息如下:') print('=' * 30) print('学号 姓名 性别 手机号码') for row in cursor: print("%s %s %s %s" % (row[0], row[1], row[2], row[3])) else: print('暂无学生信息') # 定义主函数 def main(): while True: print_menu() key = input('请输入对应的功能的数字:') if key == '1': # 添加学生信息 add_stu_info() elif key == '2': # 删除学生信息 del_stu_info() elif key == '3': # 修改学生信息 rev_stu_info() elif key == '4': # 显示所有学生信息 show_stu_info() elif key == '0': # 退出系统 quit_confirm = input('真的要退出么?(Yes/No):') if quit_confirm.lower() == 'yes': break elif quit_confirm.lower() == 'no': continue else: print('输入有误,请重新输入') else: print('输入有误,请重新输入') if __name__ == '__main__': main() ```
Pre:
2023-02-12课堂代码
Next:
2022-12-25课件-学生管理系统
0
likes
11
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.