数据库版的学生管理系统

import MySQLdb #导入MySQL模块

class Student: #创建student类
def __init__(self):
self.db = None
self.cursor = None
self.connect() #链接数据库

def connect(self):
self.db = MySQLdb.connect(host = 'localhost',user = 'root',passwd = '123456',db = 'student_1')
self.cursor = self.db.cursor()

def start(self):#主菜单
while True:
self.menu1()
num = raw_input('请输入要选择的功能:')
print num.isdigit()
if num.isdigit() == False:
continue
num = int(num)
if num == 1:
self.showStudent()
elif num == 2:
self.addStudent()
elif num == 3:
self.delStudent()
elif num == 4:
self.updateStudent()
elif num == 5:
print '已成功退出系统'
self.close()
break

def showStudent(self):#查看学生
while True:
self.menu2()
num = input('请选择功能:(0—3)')
if num == 1:
print 'ID 姓名 年龄 性别'
self.cursor.execute('select * from student')
for i in self.cursor.fetchall():
print i[0],i[1],i[2],i[3]
elif num == 2:
sid = input('请输入要查询的ID:')
self.cursor.execute('select * from student where id = %d'%sid)
if self.cursor.rowcount == 0:
print '没有相关学生'
else:
print 'ID 姓名 年龄 性别'
for i in self.cursor:
print i[0],i[1],i[2],i[3]
elif num == 3:
sname = raw_input('请输入姓名:')
self.cursor.execute("select * from student where name = '%s'"%sname)
if self.cursor.rowcount == 0:
print '没有相关学生'
else:
print 'ID 姓名 年龄 性别'
for i in self.cursor.fetchall():
print i[0],i[1],i[2],i[3]
else:
self.start()
break

def addStudent(self):#添加学生
while True:
sname = raw_input('请输入学生的姓名:')
sage = raw_input('请输入学生的年龄:')
ssax = raw_input('请输入学生的性别:')
try:
sql = 'insert into student(name,age,sax) values(%s,%s,%s)'
self.cursor.execute(sql,(sname,sage,ssax))
self.db.commit()
print '添加数据库成功'
except:
self.db.rollback()
print '添加数据库失败'
q = raw_input('任意键继续,Q键退出').lower()
if q == 'q':
break

def delStudent(self):#删除学生
self.menu3()
num = input('请选择需要的功能:')
if num == 1:
sid = raw_input('请输入一个ID或者一组ID:')
list1 = sid.split(' ')
print list1
list2 = map(lambda x:(int(x),),list1)
print list2
sql ='delete from student where id =%s'
try:
self.cursor.executemany(sql,list2)
self.db.commit()
print '删除成功'
except:
print '删除失败'
self.db.rollback()
elif num == 2:
sname = raw_input('请输入要删除的学生姓名:')
try:
self.cursor.execute("delete from student where name ='%s'"%sname)
self.db.commit()
print '删除成功!'
except:
print '删除失败!'
self.db.rollback()
else:
pass

def updateStudent(self):#修改学生信息
sid = input('请输入要修改的ID:')
sname = raw_input('请输入新的姓名:')
sage = raw_input('请输入新的年龄:')
ssax = raw_input('请输入新的性别:')
try:
self.cursor.execute("update student set name='%s',age=%s,sax='%s' where id = %s"%(sname,sage,ssax,sid))
self.db.commit()
print '修改成功!'
except:
print '修改失败!'
self.db.rollback()

def menu1(self):
print """
---------------学生管理系统---------------
1 查询学生
2 添加学生
3 删除学生
4 修改学生
5 退出系统
------------------------------------------
"""

def menu2(self):
print """
----------查询学生----------
0 返回主菜单
1 查询所有学生
2 按ID查询
3 按姓名查询
----------------------------
"""
def menu3(self):
print """
----------删除学生----------
0 返回主菜单
1 按ID删除
2 按姓名删除
"""

def close(self):
self.db.close()

if __name__ == '__main__':
student =Student()
student.start()

转载于:https://www.cnblogs.com/a1440355978/p/7079320.html

你可能感兴趣的:(数据库版的学生管理系统)