pythonmysql实现学⽣成绩管理系统
这学期在学python,感觉想写⼀个东西来巩固⾃⼰的基础,因为⼤⼆的时候我看过python,所以还是⼀共花了⼏个⼩时写了⼀个基于mysql的成绩管理系统,这个东西其实拿不出⼿,不过就当复习基本了。
1 、⾸先如果你python中没安装mysql的驱动,还是要打开cmd命令⾏安装⼀下才可以使⽤:
pip3 install PyMySQL
2 、创建数据库studentdb,你可以在图形化界⾯sqlyog中创建:
3 、然后在数据库中创建表st
4 、python连接数据库的核⼼代码:
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
user就是你⾃⼰设置的⽤户名,password就是你⾃⼰设置的密码,database就是你刚刚设置的数据库
5 、另外你也可以选择mysql命令⾏创建数据库以及数据表都是可以的
6 、完整代码如下:
import pymysql
def menu():
print("\t\t学⽣信息管理系统\t\t")
print("\t\t1 添加学⽣\t\t")
print("\t\t2 删除学⽣\t\t")
print("\t\t3 修改学⽣\t\t")
print("\t\t4 按成绩降序排序:\t\t")
print("\t\t5 按成绩升序排序:\t\t")
print("\t\t6 查询:\t\t")
print("\t\t7 退出程序\t\t")
pass
def insert():
print("插⼊学⽣信息")
name = input("请输⼊学⽣姓名:")
sex = input("请输⼊学⽣性别:")
snum = input("请输⼊学⽣学号:")
chinese = int(input("请输⼊学⽣语⽂成绩:"))
math = int(input("请输⼊学⽣数学成绩:"))
stotal = int(input("请输⼊学⽣总成绩:"))
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql = "insert into st(sname,ssex,snum,chinese,math,stotal) values('%s','%s','%s',%d,%d,%d)"%(name,sex,snum,chinese,math,stotal)
try:
dbmit()
except:
print("error")
db.close()
pass
def delete():
while 1:
print("删除学⽣信息")
choose1 = int(input("请输⼊您的删除⽅式:1 按姓名 2 按学号: "))
if choose1 == 1:
print("按姓名删除")
strname = input("请输⼊删除学⽣的名字:")
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql1 = "select *from st where sname='%s'"%(strname)
wcount>0:
try:
sql = "DELETE FROM st WHERE sname = '%s'" % (strname) ute(sql)
dbmit()
print("删除姓名为%s的学⽣信息成功" % (strname))
break
except:
print("error")
db.close()
else:
print("没有这个学⽣的信息,输⼊错误")
break
elif choose1 == 2:
print("按学号删除")
strnum = input("请输⼊删除学⽣的学号:")
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql1 = "select *from st where snum='%s'"%(strnum)
wcount>0:
sql = "DELETE FROM st WHERE snum = '%s'" % (strnum)
try:
dbmit()
print("删除学号为%s学⽣的信息成功" % (strnum))
break
except:
print("error")
db.close()
else:
print("没有这个学⽣的信息,输⼊错误")
break
else:
print("输⼊错误,重新输⼊")
break
pass
pass
pass
def change():
while 1:
print("改变学⽣信息")
myname = input("请输⼊改变学⽣的名字:")
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql1 = "select *from st where sname='%s'"%(myname)
results = cursor.fetchall()
name12='0'
for row in results:
name12 = row[0]
if myname == name12:
try:
sex = input("请输⼊学⽣性别:")
snum = input("请输⼊学⽣学号:")
chinese = int(input("请输⼊学⽣语⽂成绩:"))
math = int(input("请输⼊学⽣数学成绩:"))
stotal = int(input("请输⼊学⽣总成绩:"))
#有问题解决不了
sql3 = "update st set ssex ='%s',snum='%s',chinese=%d,math=%d,stotal=%d where sname='%s'"%(sex,snum,chinese,math,stotal,name12) ute(sql3)
dbmit()
print("修改姓名为%s学⽣的信息成功"%(myname))
break
except:
print("error")
db.close()
break
else:
print("没有这个学⽣的信息,输⼊错误")
break
pass
pass
pass
def sortbyscoredesc():
print("按成绩降序")
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql = "select * from st order by stotal desc"
try:
results = cursor.fetchall()
for row in results:
name1 = row[0]
sex1 = row[1]
num1 = row[2]
chinses1 = row[3]
math1 = row[4]
total1 = row[5]
# 打印结果
print("学⽣姓名:%s\t,学⽣性别:%s\t,学⽣学号:%s\t,语⽂成绩:%d\t,数学成绩:%d\t,总成绩:%d\t" % \
(name1, sex1, num1, chinses1, math1, total1))
dbmit()
except:
print("error")
db.close()
pass
def sortbyscore():
print("按成绩升序排序")
db = t(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql = "select * from st order by stotal asc "
try:
results = cursor.fetchall()
for row in results:
name1 = row[0]
sex1 = row[1]
num1 = row[2]
chinses1 = row[3]
math1 = row[4]
total1 = row[5]
# 打印结果
print("学⽣姓名:%s\t,学⽣性别:%s\t,学⽣学号:%s\t,语⽂成绩:%d\t,数学成绩:%d\t,总成绩:%d\t" % \
(name1, sex1, num1, chinses1, math1, total1))
dbmit()
except:
print("error")
db.close()
pass
def select():
print("查询学⽣信息")
db = t(host='localhost',user='root',password='333',database='studentdb')
cursor = db.cursor()
sql = "select * from st"
try:
results = cursor.fetchall()
for row in results:
name1 = row[0]
sex1 = row[1]
num1 = row[2]
chinses1 = row[3]
math1 = row[4]
total1 = row[5]
# 打印结果
print("学⽣姓名:%s\t,学⽣性别:%s\t,学⽣学号:%s\t,语⽂成绩:%d\t,数学成绩:%d\t,总成绩:%d\t" % \
(name1,sex1,num1,chinses1,math1,total1))
dbmit()
except:
print("error")
db.close()
pass
def exitexe():
while 1:
print("确定要退出系统吗?退出(y) 不退出(n)")
c = input("请输⼊您的选择aaaa:\t")
if c == 'y':
exit()
elif c == 'n':
break
else:
print("输⼊有误重新输⼊")
pass
pass
if __name__ == '__main__':
while True:
menu()
choose = int(input("请输⼊您的选择:\t"))
if choose == 1:
insert()
elif choose == 2:
delete()
elif choose == 3:
change()
elif choose == 4:
sortbyscoredesc()
elif choose == 5:
sortbyscore()
elif choose == 6:
select()
elif choose == 7:
exitexe()
pass
pass
7 、运⾏结果如下:
8 、总结:
在这个案例中,我遇到的问题就是在sql语句和python间,有时候忘记commit,就要测试⼗⼏分钟来解决,然后就是如何设置⽤户输⼊错误指令的问题,我⽤了while循环和break pass来解决,总体上这个项⽬不难,对初学python的⼈很有⽤,我仅⽤了最基本的语法,元组,列表集合基本没⽤,如果你有更好的想法私信我,下⼀步我将它开发为⼀个图形化界⾯的系统。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论