请用python语言,mysql数据库编写一个能实现记录人员流动,管理人员出入,只有学生和老师能进入学校的门禁系统
时间: 2024-03-02 21:48:28 浏览: 71
Python基于mysql实现学生管理系统
5星 · 资源好评率100%
好的,以下是一个简单的基于Python和MySQL的门禁系统示例,它记录人员流动、管理人员出入,并只允许学生和老师进入学校。请注意,这只是一个示例,您需要根据您的需求进行修改和扩展。
首先,您需要安装 `mysql-connector-python` 库:
```python
pip install mysql-connector-python
```
然后,您需要创建一个MySQL数据库,例如 `access_control`,并创建以下三个表格:
```sql
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`type` enum('student','teacher','staff') NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `entries` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`entry_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`exit_time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_user_entry` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
CREATE TABLE `doors` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
```
这三个表格分别用于存储用户信息、进出门禁记录和门禁门的信息。
接下来,您可以使用以下Python代码来连接MySQL数据库,创建一个新用户,并将其添加到 `users` 表格中:
```python
import mysql.connector
# Connect to the database
cnx = mysql.connector.connect(user='your_username', password='your_password',
host='your_hostname',
database='access_control')
# Create a new user
user = {
'name': 'Alice',
'type': 'student'
}
# Insert the user into the database
cursor = cnx.cursor()
add_user = ("INSERT INTO users "
"(name, type) "
"VALUES (%(name)s, %(type)s)")
cursor.execute(add_user, user)
cnx.commit()
cursor.close()
cnx.close()
```
接下来,您可以使用以下Python代码来记录用户进出门禁:
```python
import mysql.connector
import datetime
# Connect to the database
cnx = mysql.connector.connect(user='your_username', password='your_password',
host='your_hostname',
database='access_control')
# Get the user ID by name
cursor = cnx.cursor()
query = ("SELECT id, type FROM users WHERE name = %s")
cursor.execute(query, ('Alice',))
user = cursor.fetchone()
if not user:
print("User not found")
cursor.close()
cnx.close()
exit()
# Get the door ID by name
query = ("SELECT id FROM doors WHERE name = %s")
cursor.execute(query, ('Main Door',))
door = cursor.fetchone()
if not door:
print("Door not found")
cursor.close()
cnx.close()
exit()
# Check if the user is allowed to enter the door
if user[1] not in ['student', 'teacher']:
print("User not allowed to enter")
cursor.close()
cnx.close()
exit()
# Check if the user has already entered the door
query = ("SELECT id, entry_time, exit_time FROM entries WHERE user_id = %s AND exit_time IS NULL")
cursor.execute(query, (user[0],))
entry = cursor.fetchone()
if entry:
# User has already entered, so record the exit time
query = ("UPDATE entries SET exit_time = %s WHERE id = %s")
cursor.execute(query, (datetime.datetime.now(), entry[0]))
cnx.commit()
print("User exited at", datetime.datetime.now())
else:
# User has not entered yet, so record the entry time
query = ("INSERT INTO entries (user_id, door_id, entry_time) "
"VALUES (%s, %s, %s)")
cursor.execute(query, (user[0], door[0], datetime.datetime.now()))
cnx.commit()
print("User entered at", datetime.datetime.now())
cursor.close()
cnx.close()
```
以上代码会记录 Alice 进出门禁,并记录进出门禁的时间。如果 Alice 进入了门禁,它会打印 "User entered at [datetime]";如果 Alice 退出了门禁,它会打印 "User exited at [datetime]"。如果 Alice 不是学生或老师,或者门禁不存在,它会打印相应的错误消息。
请注意,以上代码只是一个简单的示例,您需要进行修改和扩展,以满足您的需求。例如,您可能需要添加更多的门禁门、用户信息和功能。
阅读全文