输出代码实现以下功能,预订房间(调用db_hotel数据库tab_room表中的rtype作为关键词预订房间成功后rstate-1当rstate为0时显示预订失败房间已无预订成功后显示当时时间,退房时rstate+1显示当时时间将tab_room表的内容已经预订或退房的情况和时间打印到一个新表中使用mysql.connector和tk
时间: 2024-03-11 13:50:41 浏览: 67
酒店管理系统 酒店预订系统 可实现预订,退房
4星 · 用户满意度95%
以下是实现你所需功能的示例代码,需要先安装 `mysql-connector-python` 库:
```python
import mysql.connector
from tkinter import *
from datetime import datetime
# Connect to MySQL database
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="db_hotel"
)
# Create a cursor object
mycursor = mydb.cursor()
# Create a new table for storing reservations and checkouts
mycursor.execute("CREATE TABLE IF NOT EXISTS reservations (rid INT AUTO_INCREMENT PRIMARY KEY, rtype VARCHAR(255), status VARCHAR(255), date_time DATETIME)")
# Define function for booking a room
def book_room():
# Retrieve the room type from the entry widget
rtype = room_type_entry.get()
# Check if a room of the specified type is available
mycursor.execute("SELECT * FROM tab_room WHERE rtype=%s AND rstate=0", (rtype,))
room = mycursor.fetchone()
if room:
# Update the room state to reserved
mycursor.execute("UPDATE tab_room SET rstate=1 WHERE rid=%s", (room[0],))
mydb.commit()
# Add a new row to the reservations table
mycursor.execute("INSERT INTO reservations (rtype, status, date_time) VALUES (%s, %s, %s)", (rtype, "reserved", datetime.now()))
mydb.commit()
# Display a success message
status_label.config(text="Room reserved successfully at " + str(datetime.now()))
else:
# Display a failure message
status_label.config(text="No available rooms of type " + rtype)
# Define function for checking out a room
def checkout_room():
# Retrieve the room type from the entry widget
rtype = room_type_entry.get()
# Check if a room of the specified type is reserved
mycursor.execute("SELECT * FROM tab_room WHERE rtype=%s AND rstate=1", (rtype,))
room = mycursor.fetchone()
if room:
# Update the room state to vacant
mycursor.execute("UPDATE tab_room SET rstate=0 WHERE rid=%s", (room[0],))
mydb.commit()
# Add a new row to the reservations table
mycursor.execute("INSERT INTO reservations (rtype, status, date_time) VALUES (%s, %s, %s)", (rtype, "checked out", datetime.now()))
mydb.commit()
# Display a success message
status_label.config(text="Room checked out successfully at " + str(datetime.now()))
else:
# Display a failure message
status_label.config(text="No reserved rooms of type " + rtype)
# Create a Tkinter window
root = Tk()
# Create entry widget for room type
room_type_entry = Entry(root)
room_type_entry.grid(row=0, column=0)
# Create button for booking a room
book_button = Button(root, text="Book Room", command=book_room)
book_button.grid(row=0, column=1)
# Create button for checking out a room
checkout_button = Button(root, text="Check Out Room", command=checkout_room)
checkout_button.grid(row=1, column=1)
# Create label for displaying status messages
status_label = Label(root, text="")
status_label.grid(row=2, column=0, columnspan=2)
# Start the Tkinter event loop
root.mainloop()
# Print the reservations table
mycursor.execute("SELECT * FROM reservations")
reservations = mycursor.fetchall()
for reservation in reservations:
print(reservation)
```
在这个例子中,我们首先创建了一个名为 `reservations` 的新表,用来存储预订和退房的信息。然后,我们定义了两个函数,一个用来预订房间,另一个用来退房。这些函数使用 `mysql.connector` 库与数据库交互,更新 `tab_room` 表中的状态,并将预订或退房的信息添加到 `reservations` 表中。最后,我们使用 Tkinter 创建了一个窗口,包含一个输入框和两个按钮,用来触发预订或退房操作。我们还在窗口中添加了一个标签,用来显示操作的结果。在程序结束后,我们从 `reservations` 表中检索数据,并将其打印到控制台上。请记得将代码中的“yourusername”和“yourpassword”替换为你自己的MySQL用户名和密码。
阅读全文