请用sql语句和python语句写出一个完整的高校宿舍管理系统,有登录界面的
时间: 2024-03-21 08:42:44 浏览: 57
好的,下面是一个基于 SQL 和 Python 的简单的高校宿舍管理系统,包含登录功能和学生信息管理功能。
SQL 代码:
```
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL,
age INT NOT NULL,
dormitory VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
```
Python 代码:
```
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# 创建游标对象
mycursor = mydb.cursor()
# 登录界面
def login():
username = input("请输入用户名:")
password = input("请输入密码:")
# 验证用户信息
sql = "SELECT * FROM users WHERE username = %s AND password = %s"
val = (username, password)
mycursor.execute(sql, val)
myresult = mycursor.fetchone()
if myresult:
print("登录成功!")
return True
else:
print("用户名或密码错误!")
return False
# 学生信息管理界面
def student_management():
print("欢迎进入学生信息管理系统!")
while True:
print("请选择操作:")
print("1. 添加学生")
print("2. 修改学生信息")
print("3. 删除学生")
print("4. 查询学生信息")
print("5. 退出")
choice = input("请输入操作编号:")
if choice == "1":
add_student()
elif choice == "2":
update_student()
elif choice == "3":
delete_student()
elif choice == "4":
query_student()
elif choice == "5":
print("谢谢使用!")
break
else:
print("无效的操作编号,请重新输入!")
# 添加学生
def add_student():
name = input("请输入学生姓名:")
gender = input("请输入学生性别:")
age = input("请输入学生年龄:")
dormitory = input("请输入学生宿舍:")
phone = input("请输入学生电话:")
email = input("请输入学生邮箱:")
sql = "INSERT INTO students (name, gender, age, dormitory, phone, email) VALUES (%s, %s, %s, %s, %s, %s)"
val = (name, gender, age, dormitory, phone, email)
mycursor.execute(sql, val)
mydb.commit()
print("添加成功!")
# 修改学生信息
def update_student():
id = input("请输入要修改的学生 ID:")
sql = "SELECT * FROM students WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
myresult = mycursor.fetchone()
if myresult:
name = input("请输入学生姓名:")
gender = input("请输入学生性别:")
age = input("请输入学生年龄:")
dormitory = input("请输入学生宿舍:")
phone = input("请输入学生电话:")
email = input("请输入学生邮箱:")
sql = "UPDATE students SET name = %s, gender = %s, age = %s, dormitory = %s, phone = %s, email = %s WHERE id = %s"
val = (name, gender, age, dormitory, phone, email, id)
mycursor.execute(sql, val)
mydb.commit()
print("修改成功!")
else:
print("学生不存在!")
# 删除学生
def delete_student():
id = input("请输入要删除的学生 ID:")
sql = "SELECT * FROM students WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
myresult = mycursor.fetchone()
if myresult:
sql = "DELETE FROM students WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
print("删除成功!")
else:
print("学生不存在!")
# 查询学生信息
def query_student():
print("请选择查询方式:")
print("1. 按姓名查询")
print("2. 按宿舍查询")
choice = input("请输入查询方式编号:")
if choice == "1":
name = input("请输入学生姓名:")
sql = "SELECT * FROM students WHERE name = %s"
val = (name,)
mycursor.execute(sql, val)
myresult = mycursor.fetchall()
if myresult:
for student in myresult:
print(student)
else:
print("未找到学生!")
elif choice == "2":
dormitory = input("请输入学生宿舍:")
sql = "SELECT * FROM students WHERE dormitory = %s"
val = (dormitory,)
mycursor.execute(sql, val)
myresult = mycursor.fetchall()
if myresult:
for student in myresult:
print(student)
else:
print("未找到学生!")
else:
print("无效的操作编号,请重新输入!")
# 主程序
def main():
while True:
if login():
student_management()
break
if __name__ == "__main__":
main()
```
这个高校宿舍管理系统包含了登录界面和学生信息管理功能,您可以根据需求进行修改和扩展。
阅读全文