Python期末小项目之学生管理系统分享

import sqlite3

#链接本地数据库

def database():

    conn = None

    conn1 = None

    conn = sqlite3.connect('user.db')

    cursor = conn.cursor()

 

    conn1 = sqlite3.connect('stu.db')

    cursor1 = conn1.cursor()

#登录界面

def login():

    conn = sqlite3.connect('user.db')

    cursor = conn.cursor()

    username = input("请输入用户名:")

    password = input("请输入密码:")

    cursor.execute("SELECT * FROM user WHERE username=? AND password=?", (username, password))

 

    values = cursor.fetchall()

    if len(values) > 0:

            print("登录成功")

            return True

    elif len(values) < 0:

            print("账号密码不存在")

            return False

    else:

            print("账号或密码错误,请检查后重新输入!")

            return False

 

 

 

#注册功能

def register():

    conn = sqlite3.connect('user.db')

    cursor = conn.cursor()

 

    username = input("请输入用户名:")

    password = input("请输入密码:")

    cursor.execute("select * from user where username=?", (username,))

    values = cursor.fetchall()

    if len(values) > 0:

        print("用户名已存在")

    else:

        cursor.execute("insert into user (username, password) values (?, ?)", (username, password))

        conn.commit()

        print("注册成功")

 

#添加学生信息

def add_student():

    conn1 = sqlite3.connect('stu.db')

    cursor1 = conn1.cursor()

    name = str(input("请输入学生姓名:"))

    age = int(input("请输入年龄:"))

    score = int(input("请输入学生成绩:"))

    cursor1.execute("select * from stu where name=? and age=? and score=?", (name,age,score))

    data = cursor1.fetchone()

    if data:

        print("该学生已存在,请重新输入!")

    else:

        cursor1.execute("insert into stu(name, age, score) values(?, ?, ?)", (name, age, score))

    print("添加成功!")

    conn1.commit()

    cursor1.close()

    conn1.close()

 

#删除学生

def delete_student():

    conn1 = sqlite3.connect('stu.db')

    cursor1 = conn1.cursor()

    name = str(input("请输入学生姓名:"))

    cursor1.execute("select * from stu where name=?", (name,))

    data = cursor1.fetchone()

    if data:

        cursor1.execute("delete from stu where name=?", (name,))

        conn1.commit()

        print("删除成功!")

    else:

        print("该学生不存在,请重新输入!")

    conn1.commit()

    cursor1.close()

    conn1.close()

#修改学生信息

def modify_student():

    conn1 = sqlite3.connect('stu.db')

    cursor1 = conn1.cursor()

    name = str(input("请输入学生姓名:"))

    cursor1.execute("select * from stu where name=?", (name,)) #修改这里为正确的表名student

    data = cursor1.fetchone()

    if data:

        new_age = int(input("请输入新的年龄:"))

        new_score = int(input("请输入新的成绩:"))

        cursor1.execute("update stu set age=?, score=? where name=?", (new_age, new_score, name))

        conn1.commit()

        print("修改成功!")

    else:

        print("该学生不存在,请重新输入!")

    conn1.commit()

    cursor1.close()

    conn1.close()

#查找学生信息

def find_student():

    conn1 = sqlite3.connect('stu.db')

    cursor1 = conn1.cursor()

    name = str(input("请输入学生姓名:"))

    cursor1.execute("select * from stu where name=?", (name,))

    data = cursor1.fetchone()

    if data:

        print("学生信息如下:")

        print("姓名:", data[0])

        print("年龄:", data[1])

        print("成绩:", data[2])

    else:

        print("该学生不存在,请重新输入!")

    conn1.commit()

    cursor1.close()

    conn1.close()

#显示所有学生信息

def show_all_students():

    conn1 = sqlite3.connect('stu.db')

    cursor1 = conn1.cursor()

    cursor1.execute("select * from stu")

    data = cursor1.fetchall()

    if data:

        print("所有学生信息如下:")

        for row in data:

            print("姓名:", row[0])

            print("年龄:", row[1])

            print("成绩:", row[2])

            print("---"*20)

    else:

        print("暂无学生信息!")

    conn1.commit()

    cursor1.close()

    conn1.close()

#主菜单

def main():

    login_status = False # 添加一个变量来存储登录状态

    try:

        while True:

            print("***" * 10)

            print("1.注册")

            print("2.登录")

            print("3.退出")

            print("4.注销账户")

            print("***" * 10)

 

            choice = input("请输入你的选择:")

            if choice == '1':

                register()

            elif choice == '2':

                login_status = login() # 更新登录状态

                if login_status: # 如果登录成功进入下一个

                    print("欢迎使用学生管理系统")

                while login_status: # 根据登录状态来决定是否进入学生管理系统

                    print("---"*20)

                    print("1.添加学生")

                    print("2.删除学生")

                    print("3.修改学生")

                    print("4.查询学生")

                    print("5.显示所有学生信息")

                    print("6.退出")

                    print("---" * 20)

                    choice = input("请输入你的选择:\n")

                    if choice == '1':

                        add_student()

                    elif choice == '2':

                        delete_student()

                    elif choice == '3':

                        modify_student()

                    elif choice == '4':

                        find_student()

                    elif choice == '5':

                        show_all_students()

                    elif choice == '6':

                        break

            elif choice == '3':

                break

            elif choice == '4':

                username = input("请输入你的账号")

                password = input("请输入你的密码")

                conn = sqlite3.connect('user.db')

                cursor = conn.cursor()

                cursor.execute("delete from user where username=? and password=?", (username, password))

                conn.commit()

                print("注销成功")

            else:

                print("您输入的选项有问题,请检查后重新输入!!!")

    except KeyboardInterrupt:

        print("程序已安全退出")

 

if __name__ == '__main__':

    main()

  • 17
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值