用python和mysql语言编写一个车位管理系统
时间: 2023-09-08 22:09:56 浏览: 116
好的,下面是一个简单的车位管理系统的 Python 代码实现,使用了 tkinter 和 mysql-connector-python 库:
```python
import tkinter as tk
import mysql.connector
# 连接 MySQL 数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# 创建车位表
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS parking_spaces (id INT AUTO_INCREMENT PRIMARY KEY, number VARCHAR(255), status BOOLEAN)")
# 创建 tkinter 窗口
root = tk.Tk()
root.title("车位管理系统")
# 显示车位状态
def show_status():
# 查询车位信息
mycursor.execute("SELECT * FROM parking_spaces")
spaces = mycursor.fetchall()
# 显示车位状态
for i, space in enumerate(spaces):
num_label = tk.Label(root, text=space[1])
num_label.grid(row=i+1, column=0)
status_label = tk.Label(root, text="空闲" if space[2] else "已预定")
status_label.grid(row=i+1, column=1)
# 预定车位
def reserve_space(number):
# 查询车位状态
mycursor.execute("SELECT * FROM parking_spaces WHERE number=%s", (number,))
space = mycursor.fetchone()
# 如果车位为空闲,则预定车位
if space[2]:
mycursor.execute("UPDATE parking_spaces SET status=0 WHERE number=%s", (number,))
mydb.commit()
tk.messagebox.showinfo("提示", "车位预定成功!")
else:
tk.messagebox.showerror("错误", "该车位已被预定!")
# 释放车位
def release_space(number):
# 查询车位状态
mycursor.execute("SELECT * FROM parking_spaces WHERE number=%s", (number,))
space = mycursor.fetchone()
# 如果车位已预定或已停车,则释放车位
if not space[2]:
mycursor.execute("UPDATE parking_spaces SET status=1 WHERE number=%s", (number,))
mydb.commit()
tk.messagebox.showinfo("提示", "车位释放成功!")
else:
tk.messagebox.showerror("错误", "该车位为空闲!")
# 查询停车记录
def search_record(number):
# 查询停车记录
mycursor.execute("SELECT * FROM parking_records WHERE number=%s", (number,))
records = mycursor.fetchall()
# 显示停车记录
for i, record in enumerate(records):
time_label = tk.Label(root, text=record[2])
time_label.grid(row=i+1, column=2)
cost_label = tk.Label(root, text=record[3])
cost_label.grid(row=i+1, column=3)
# 创建界面元素
num_title = tk.Label(root, text="车位编号")
num_title.grid(row=0, column=0)
status_title = tk.Label(root, text="车位状态")
status_title.grid(row=0, column=1)
time_title = tk.Label(root, text="停车时间")
time_title.grid(row=0, column=2)
cost_title = tk.Label(root, text="停车费用")
cost_title.grid(row=0, column=3)
show_status_btn = tk.Button(root, text="显示车位状态", command=show_status)
show_status_btn.grid(row=0, column=4)
reserve_label = tk.Label(root, text="预定车位")
reserve_label.grid(row=8, column=0)
reserve_entry = tk.Entry(root)
reserve_entry.grid(row=8, column=1)
reserve_btn = tk.Button(root, text="预定", command=lambda: reserve_space(reserve_entry.get()))
reserve_btn.grid(row=8, column=2)
release_label = tk.Label(root, text="释放车位")
release_label.grid(row=9, column=0)
release_entry = tk.Entry(root)
release_entry.grid(row=9, column=1)
release_btn = tk.Button(root, text="释放", command=lambda: release_space(release_entry.get()))
release_btn.grid(row=9, column=2)
search_label = tk.Label(root, text="查询停车记录")
search_label.grid(row=10, column=0)
search_entry = tk.Entry(root)
search_entry.grid(row=10, column=1)
search_btn = tk.Button(root, text="查询", command=lambda: search_record(search_entry.get()))
search_btn.grid(row=10, column=2)
# 运行界面
root.mainloop()
```
上面的代码实现了一个简单的车位管理系统,并且包含了车位状态显示、车位预定、车位释放、查询停车记录等功能。当用户预定或释放车位时,系统会将相应的车位状态更改为已预定或空闲,并将停车信息存储在 MySQL 数据库中。当用户查询停车记录时,系统会在 MySQL 数据库中查询相应的记录并在用户界面上显示。
当然,这只是一个简单的示例,具体实现细节需要根据实际情况进行调整。希望以上代码能够对你有所帮助。
阅读全文