这学期在学python,感觉想写一个东西来巩固自己的基础,因为大二的时候我看过python,所以还是一共花了几个小时写了一个基于mysql的成绩管理系统,这个东西其实拿不出手,不过就当复习基本了。
1 、首先如果你python中没安装mysql的驱动,还是要打开cmd命令行安装一下才可以使用:
pip3 install PyMySQL
2 、创建数据库studentdb,你可以在图形化界面sqlyog中创建:
3 、然后在数据库中创建表st
4 、python连接数据库的核心代码:
db = pymysql.connect(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 = pymysql.connect(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:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("error")
db.close()
pass
def delete():
while 1:
print("删除学生信息")
choose1 = int(input("请输入您的删除方式:1 按姓名 2 按学号: "))
if choose1 == 1:
print("按姓名删除")
strname = input("请输入删除学生的名字:")
db = pymysql.connect(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql1 = "select *from st where sname='%s'"%(strname)
cursor.execute(sql1)
if cursor.rowcount>0:
try:
sql = "DELETE FROM st WHERE sname = '%s'" % (strname)
cursor.execute(sql)
db.commit()
print("删除姓名为%s的学生信息成功" % (strname))
break
except:
db.rollback()
print("error")
db.close()
else:
print("没有这个学生的信息,输入错误")
break
elif choose1 == 2:
print("按学号删除")
strnum = input("请输入删除学生的学号:")
db = pymysql.connect(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql1 = "select *from st where snum='%s'"%(strnum)
cursor.execute(sql1)
if cursor.rowcount>0:
sql = "DELETE FROM st WHERE snum = '%s'" % (strnum)
try:
cursor.execute(sql)
db.commit()
print("删除学号为%s学生的信息成功" % (strnum))
break
except:
db.rollback()
print("error")
db.close()
else:
print("没有这个学生的信息,输入错误")
break
else:
print("输入错误,重新输入")
break
pass
pass
pass
def change():
while 1:
print("改变学生信息")
myname = input("请输入改变学生的名字:")
db = pymysql.connect(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql1 = "select *from st where sname='%s'"%(myname)
cursor.execute(sql1)
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)
cursor.execute(sql3)
db.commit()
print("修改姓名为%s学生的信息成功"%(myname))
break
except:
db.rollback()
print("error")
db.close()
break
else:
print("没有这个学生的信息,输入错误")
break
pass
pass
pass
def sortbyscoredesc():
print("按成绩降序")
db = pymysql.connect(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql = "select * from st order by stotal desc"
try:
cursor.execute(sql)
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))
db.commit()
except:
db.rollback()
print("error")
db.close()
pass
def sortbyscore():
print("按成绩升序排序")
db = pymysql.connect(host='localhost',
user='root',
password='333',
database='studentdb')
cursor = db.cursor()
sql = "select * from st order by stotal asc "
try:
cursor.execute(sql)
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))
db.commit()
except:
db.rollback()
print("error")
db.close()
pass
def select():
print("查询学生信息")
db = pymysql.connect(host='localhost',user='root',password='333',database='studentdb')
cursor = db.cursor()
sql = "select * from st"
try:
cursor.execute(sql)
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))
db.commit()
except:
db.rollback()
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的人很有用,我仅用了最基本的语法,元组,列表集合基本没用,如果你有更好的想法私信我,下一步我将它开发为一个图形化界面的系统。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。