MySQL 数据库 酒店管理系统客房预订数据处理实战

MySQL 数据库阿木 发布于 2025-07-08 16 次阅读


酒店管理系统客房预订数据处理实战

随着旅游业的蓬勃发展,酒店行业也日益繁荣。为了提高酒店的服务质量和客户满意度,酒店管理系统应运而生。其中,客房预订模块是酒店管理系统的核心功能之一。本文将围绕MySQL数据库,通过实际案例,展示如何使用代码编辑模型进行酒店管理系统客房预订数据处理。

系统需求分析

在开始编写代码之前,我们需要对酒店管理系统客房预订模块的需求进行分析。以下是该模块的主要功能:

1. 客房信息管理:包括客房类型、价格、数量等信息的录入、修改和查询。

2. 预订管理:包括预订信息的录入、修改、查询和取消。

3. 客房状态管理:实时显示客房的预订状态,包括已预订、空闲、维修等。

4. 预订统计:统计预订数量、预订金额等数据。

数据库设计

为了实现上述功能,我们需要设计一个合理的数据库结构。以下是客房预订模块的数据库设计:

1. 客房信息表(rooms)

| 字段名 | 数据类型 | 说明 |

| ------------ | ------------ | -------------- |

| room_id | INT | 客房ID,主键 |

| room_type | VARCHAR(50) | 客房类型 |

| price | DECIMAL(10,2)| 客房价格 |

| quantity | INT | 客房数量 |

| status | VARCHAR(20) | 客房状态(已预订、空闲、维修等) |

2. 预订信息表(bookings)

| 字段名 | 数据类型 | 说明 |

| ------------ | ------------ | -------------- |

| booking_id | INT | 预订ID,主键 |

| room_id | INT | 客房ID,外键 |

| guest_name | VARCHAR(100) | 客人姓名 |

| contact_info | VARCHAR(200) | 联系方式 |

| check_in | DATE | 入住日期 |

| check_out | DATE | 离店日期 |

| status | VARCHAR(20) | 预订状态(已预订、已入住、已退房等) |

代码实现

1. 客房信息管理

客房信息录入

python

import mysql.connector

连接数据库


db = mysql.connector.connect(


host="localhost",


user="root",


password="password",


database="hotel_management"


)

创建游标对象


cursor = db.cursor()

客房信息


room_data = {


"room_type": "单人间",


"price": 300.00,


"quantity": 10,


"status": "空闲"


}

插入数据


sql = "INSERT INTO rooms (room_type, price, quantity, status) VALUES (%s, %s, %s, %s)"


cursor.execute(sql, (room_data["room_type"], room_data["price"], room_data["quantity"], room_data["status"]))

提交事务


db.commit()

关闭游标和数据库连接


cursor.close()


db.close()


客房信息查询

python

import mysql.connector

连接数据库


db = mysql.connector.connect(


host="localhost",


user="root",


password="password",


database="hotel_management"


)

创建游标对象


cursor = db.cursor()

查询条件


room_type = "单人间"

查询数据


sql = "SELECT FROM rooms WHERE room_type = %s"


cursor.execute(sql, (room_type,))

获取查询结果


results = cursor.fetchall()

打印查询结果


for row in results:


print(row)

关闭游标和数据库连接


cursor.close()


db.close()


2. 预订管理

预订信息录入

python

import mysql.connector

连接数据库


db = mysql.connector.connect(


host="localhost",


user="root",


password="password",


database="hotel_management"


)

创建游标对象


cursor = db.cursor()

预订信息


booking_data = {


"room_id": 1,


"guest_name": "张三",


"contact_info": "13800138000",


"check_in": "2022-01-01",


"check_out": "2022-01-03",


"status": "已预订"


}

插入数据


sql = "INSERT INTO bookings (room_id, guest_name, contact_info, check_in, check_out, status) VALUES (%s, %s, %s, %s, %s, %s)"


cursor.execute(sql, (booking_data["room_id"], booking_data["guest_name"], booking_data["contact_info"], booking_data["check_in"], booking_data["check_out"], booking_data["status"]))

提交事务


db.commit()

关闭游标和数据库连接


cursor.close()


db.close()


预订信息查询

python

import mysql.connector

连接数据库


db = mysql.connector.connect(


host="localhost",


user="root",


password="password",


database="hotel_management"


)

创建游标对象


cursor = db.cursor()

查询条件


guest_name = "张三"

查询数据


sql = "SELECT FROM bookings WHERE guest_name = %s"


cursor.execute(sql, (guest_name,))

获取查询结果


results = cursor.fetchall()

打印查询结果


for row in results:


print(row)

关闭游标和数据库连接


cursor.close()


db.close()


3. 客房状态管理

客房状态更新

python

import mysql.connector

连接数据库


db = mysql.connector.connect(


host="localhost",


user="root",


password="password",


database="hotel_management"


)

创建游标对象


cursor = db.cursor()

更新客房状态


sql = "UPDATE rooms SET status = %s WHERE room_id = %s"


cursor.execute(sql, ("已预订", 1))

提交事务


db.commit()

关闭游标和数据库连接


cursor.close()


db.close()


4. 预订统计

预订数量统计

python

import mysql.connector

连接数据库


db = mysql.connector.connect(


host="localhost",


user="root",


password="password",


database="hotel_management"


)

创建游标对象


cursor = db.cursor()

统计预订数量


sql = "SELECT COUNT() FROM bookings WHERE status = '已预订'"


cursor.execute(sql)

获取查询结果


result = cursor.fetchone()


print("预订数量:", result[0])

关闭游标和数据库连接


cursor.close()


db.close()


总结

本文通过实际案例,展示了如何使用MySQL数据库和Python代码实现酒店管理系统客房预订数据处理。在实际开发过程中,可以根据需求对数据库结构和代码进行优化和扩展。希望本文对您有所帮助。