Python_学生管理普通版和sqlite3类和对象版
程序员文章站
2022-09-05 13:10:38
学生管理普通版student_list=[]#[[一个学员信息student],[],[],[]] 大列表是student_listdef wu(): print('没有学员信息,无法进行删除操作!') returndef xu(type): num = int(input('请选择要%s的学员序号:'%type)) while num not ......
学生管理普通版
student_list=[]
#[[一个学员信息student],[],[],[]] 大列表是student_list
def wu():
print('没有学员信息,无法进行删除操作!')
return
def xu(type):
num = int(input('请选择要%s的学员序号:'%type))
while num not in range(0, len(student_list)):
num = int(input('没有该序号,请重选:'))
return num
# 添加学员的函数
def add_student():
#姓名、年龄、体重、性别
name=input('请输入姓名:')
age=input('请输入年龄:')
weight=input('请输入体重:')
sex = input('请输入性别:')
#
student=[name,age,weight,sex]
#把小列表添加到所有学员的大列表中
student_list.append(student)
print('学员信息添加完成!')
def all():
print('学员信息列表')
# 遍历大列表
for x in range(0, len(student_list)):
# 根据x的值 从大列表中取出小列表
student = student_list[x]
# 从小列表中取出姓名、年龄和体重
name = student[0]
age = student[1]
weight = student[2]
sex = student[3]
print('序号:%s 姓名:%s 年龄:%s 体重:%s 性别:%s' % (x, name, age, weight, sex))
#查询学员的函数
def query_student():
if len(student_list)==0:
print('没有学员信息')
return
else:
# 1.查询所有学员
# 2.输入学员姓名查询学员 得到查询的学员的序号
print('1.查询所有学员信息')
print('2.按照学员姓名查找')
a=int(input('请选择您要查找的方式:'))
while a not in range(1,3):
a=int(input('没有该选项,请重新输入'))
if a==1:
all()
elif a==2:
for x in range(0,len(student_list)):
#根据x的值 从大列表中取出小列表
student=student_list[x]
name = student[0]
age = student[1]
weight = student[2]
sex = student[3]
ming = input('请您输入要查询学员的姓名')
if ming==name:
print('序号:%s 姓名:%s 年龄:%s 体重:%s 性别:%s' % (x, name, age, weight, sex))
else:
print('查无此人')
#修改学员的函数
def update_student():
#查询学员信息
all()
# 选择要修改的学员序号
if len(student_list)==0:
wu()
else:
num = xu('修改')
#根据选择的序号,取出对应的学员信息小列表
student=student_list[num]
new_name=input('请输入修改后的姓名(%s):'%student[0])
new_age = input('请输入修改后的年龄(%s):' % student[1])
new_weight = input('请输入修改后的体重(%s):' % student[2])
new_sex = input('请输入修改后的性别(%s):' % student[3])
# 修改小列表中的数据
student[0]=new_name
student[1]=new_age
student[2]=new_weight
student[3]=new_sex
print('----修改数据完成----')
#删除学员
def del_student():
all()
if len(student_list)==0:
wu()
else:
while True:
print('1.根据学员学号删除')
print('2.删除所有学员')
print('3.根据学员姓名删除')
print('4.退出删除')
index=input('请选择您的操作:')
index=int(index)
while index not in range(1,5):
index=int(input('没有该选项,请重新输入:'))
if index==1:
all()
num=xu('删除')
student=student_list[num]
del student_list[num]
print('---该学员信息删除成功---')
elif index==2:
#确认删除
rs=input('确定要删除所有信息?y(确定)/n(取消):')
if rs=='y':
student_list.clear()
print('---所有学员信息删除成功---')
break
else:
print('删除数据操作已取消!')
elif index==3:
for x in range(0, len(student_list)):
# 根据x的值 从大列表中取出小列表
student = student_list[x]
name = student[0]
age = student[1]
weight = student[2]
sex = student[3]
ming = input('请您输入要查询学员的姓名')
if ming == name:
del student_list[x]
else:
print('---退出删除操作---')
break
def write():
file_handle=open('student.txt',mode='w')
for student in student_list:
s=' '.join(student)
file_handle.write(s)
file_handle.write('\n')
file_handle.close()
# 引入os模块
import os
def read():
#判断文件是否存在
rs=os.path.exists('student.txt')
if rs==True:
file_handle=open('student.txt',mode='r')
contents=file_handle.readlines()
for msg in contents:
msg=msg.strip('\n')
list_1=msg.split(' ')
student_list.append(list_1)
read()
# while循环
while True:
print('1.添加学员')
print('2.查询学员')
print('3.修改学员')
print('4.删除学员')
print('0.退出')
#选择操作
num=input('请选择操作:')
num=int(num)
#判断选择的选项是否在范围内
while num not in range(0,5):
num=input('没有该选项,请重新选择:')
num=int(num)
if num ==1:
add_student()
write()
elif num==2:
query_student()
elif num ==3:
update_student()
write()
elif num ==4:
del_student()
write()
else:
print('程序已结束')
break
sqlite3、类和对象版
import sqlite3
# 声明一个数据模型类:只包含属性,不包含操作属性的函数
class StudentModel(object):
def __init__(self, db_name, table_name, field_name, field_age, field_score, field_id):
self.db_name = db_name
self.table_name = table_name
self.field_name = field_name
self.field_age = field_age
self.field_score = field_score
self.field_id = field_id
# 声明一个工具类,工具类一般只包含操作函数,不包含属性
class DBManager(object):
# 定义一个创建库和游标的函数
# stu_obj:StudentModel类生成的对象
# stu_obj.db_name:StudentModel类中的db_name属性,对应的就是数据库名称
def create_connet_and_cursor(self, stu_obj):
self.connet = sqlite3.connect(stu_obj.db_name)
self.cursor = self.connet.cursor()
# 定义一个创建表的函数
def create_table(self, stu_obj):
create_sql = "create table if not exists " + stu_obj.table_name + "(%s INTEGER PRIMARY KEY UNIQUE,%s TEXT,%s TEXT,%s TEXT )" % (
stu_obj.field_id, stu_obj.field_name, stu_obj.field_age, stu_obj.field_score)
self.cursor.execute(create_sql)
# 定义一个添加数据的函数
def insert_student_info(self, stu_obj):
name = input('请输入要添加的学员姓名:')
age = input('请输入要添加的学员年龄:')
score = input('请输入要添加的学员的成绩:')
insert_sql = "insert into " + stu_obj.table_name + "(%s,%s,%s) values ('%s','%s','%s')" % (
stu_obj.field_name, stu_obj.field_age, stu_obj.field_score, name, age, score)
self.cursor.execute(insert_sql)
# 定义一个查询数据库数据总量的函数
def get_total_count(self, stu_obj):
select_sql = "select count(*) from %s" % (stu_obj.table_name)
res = self.cursor.execute(select_sql)
count = res.fetchone()[0]
return count
# 定义一个能否查询到ID对应数据的函数
def get_id_true_or_false(self, stu_obj, number):
select_number = "select * from %s where id=%d" % (stu_obj.table_name, number)
res = self.cursor.execute(select_number)
result = res.fetchall()
return len(result)
# 定义一个查询数据的函数
def select_student_info(self, stu_obj):
count = self.get_total_count(stu_obj)
if count != 0:
select_sql = " select * from " + stu_obj.table_name
result = self.cursor.execute(select_sql)
for id, name, age, score in result:
print(id, '', name, age, score)
else:
print('学员信息为空,无法查询')
# 定义一个更改数据的函数
def update_student_info(self, stu_obj):
count = self.get_total_count(stu_obj)
if count != 0:
self.select_student_info(stu_obj)
number = int(input('请输入要修改的的学员的编号:'))
while self.get_id_true_or_false(stu_obj, number) == False:
number = int(input('编号输入错误,请重新输入要修改的的学员的编号:'))
name = input('请输入新的姓名:')
age = input('请输入新的年龄:')
score = input('请输入新的分数:')
update_sql = "update " + stu_obj.table_name + " set %s='%s',%s='%s',%s='%s' where %s=%s" % (
stu_obj.field_name, name, stu_obj.field_age, age, stu_obj.field_score, score, stu_obj.field_id, number)
self.cursor.execute(update_sql)
else:
print('学员信息为空,无法修改')
# 定义删除数据的函数
def delete_test(self, stu_obj):
count = self.get_total_count(stu_obj)
if count != 0:
self.select_student_info(stu_obj)
print('1-根据学员序号删除学员信息')
print('2-删除所有学员信息')
select_number = int(input('请输入你要操作的序号:'))
while select_number != 1 and select_number != 2:
select_number = int(input('序号输入错误,请重新输入你要操作的序号:'))
if select_number == 1:
number = int(input('请输入要删除的学员的序号:'))
while self.get_id_true_or_false(stu_obj, number) == False:
number = int(input('序号输入错误,请重新输入要删除的学员序号:'))
delete_sql = "delete from %s where id=%d" % (stu_obj.table_name, number)
else:
delete_sql = "delete from %s" % (stu_obj.table_name)
self.cursor.execute(delete_sql)
else:
print('学员信息为空,无法删除')
# 定义一个关闭数据库连接和游标的函数
def close_db_and_commit(self):
self.connet.commit()
self.cursor.close()
self.connet.close()
if __name__ == '__main__':
student = StudentModel('Student_Plus.db', 'student', 'name', 'age', 'score', 'id')
db_manager = DBManager()
db_manager.create_connet_and_cursor(student)
db_manager.create_table(student)
while True:
print('''
1-添加学员信息
2-修改学员信息
3-查询学员信息
4-删除学员信息
0-退出程序
''')
select_number = int(input('请选择操作序号:'))
while select_number < 0 or select_number > 4:
select_number = int(input('输入错误,请重新选择操作序号:'))
if select_number == 1:
db_manager.insert_student_info(student)
elif select_number == 2:
db_manager.update_student_info(student)
elif select_number == 3:
db_manager.select_student_info(student)
elif select_number == 4:
db_manager.delete_test(student)
else:
break
db_manager.close_db_and_commit()
本文地址:https://blog.csdn.net/qq_42431398/article/details/85858888