You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.0 KiB
95 lines
3.0 KiB
import pymysql
|
|
|
|
conn = pymysql.connect(host='localhost'
|
|
, user='root'
|
|
, password='20030201'
|
|
, database='student'
|
|
, charset='utf8')
|
|
|
|
cur = conn.cursor()
|
|
# 添加学生信息
|
|
def addstu(name,sex,num):
|
|
i = cur.execute("select num from stu where num=%s", num)
|
|
if i==0:
|
|
sql = "insert into stu values(%s,%s,%s)"
|
|
cur.execute(sql, (name, sex, num))
|
|
conn.commit()
|
|
print("添加成功")
|
|
else:
|
|
print("该号码存在,添加失败")
|
|
|
|
# 删除学生信息
|
|
def deletestu(num):
|
|
sql = "delete from stu where num=%s"
|
|
f = cur.execute(sql,(num))
|
|
conn.commit()
|
|
if f==0:
|
|
print("该号码不存在,删除失败")
|
|
else:
|
|
print("删除成功")
|
|
# 修改学生信息
|
|
def changestu(name,sex,num):
|
|
sql = "update stu set name=%s,sex=%s where num=%s"
|
|
f = cur.execute(sql,(name,sex,num))
|
|
conn.commit()
|
|
if f==0:
|
|
print("修改失败,该电话号码不存在")
|
|
else:
|
|
cur.execute("select * from stu where num=%s",num)
|
|
conn.commit()
|
|
print("修改成功,修改后信息为"+str(cur.fetchone()))
|
|
# 查询学生信息
|
|
def selectstu(num):
|
|
sql = "select * from stu where num=%s"
|
|
i = cur.execute(sql,num)
|
|
conn.commit()
|
|
if i==0:
|
|
print("该号码不存在")
|
|
else:
|
|
data = cur.fetchone()
|
|
print(data)
|
|
# 海象运算符3.8以后才能用
|
|
# 赋值表达式的值可以直接作为while循环的条件
|
|
# while data := cursor.fetchone()
|
|
# print(data)
|
|
print("查询成功")
|
|
# 查询所有学生信息
|
|
def selectAllStu():
|
|
cur.execute("select * from stu")
|
|
data = cur.fetchone()
|
|
while data:
|
|
print(data)
|
|
data = cur.fetchone()
|
|
while(True):
|
|
print("----------学生管理系统----------")
|
|
print("请选择如下功能:")
|
|
print("1.添加学生")
|
|
print("2.删除学生")
|
|
print("3.修改学生信息")
|
|
print("4.查询学生信息")
|
|
print("5.显示所有学生信息")
|
|
print("6.保存学生信息")
|
|
print("7.退出系统")
|
|
chose = input("请输入你需要的功能序号:")
|
|
if chose == "1":
|
|
name = input("请输入学生姓名:")
|
|
sex = input("请输入学生性别:")
|
|
num = input("请输入学生电话:")
|
|
addstu(name,sex,num)
|
|
elif chose == "2":
|
|
num = input("请输入要删除学生的电话:")
|
|
deletestu(num)
|
|
elif chose == "3":
|
|
num = input("请输入要修改学生的电话:")
|
|
name = input("请输入学生姓名:")
|
|
sex = input("请输入学生性别:")
|
|
changestu(name,sex,num)
|
|
elif chose == "4":
|
|
num = input("请输入要查询学生电话:")
|
|
selectstu(num)
|
|
elif chose =="5":
|
|
selectAllStu()
|
|
elif chose == "6":
|
|
print("学生信息保存成功")
|
|
elif chose == "7":
|
|
break |