租车信息管理系统python代码
时间: 2024-06-30 17:00:53 浏览: 98
租车管理系统源码
在创建一个租车信息管理系统(Car Rental Management System)的Python代码时,我们通常会涉及到数据库操作、用户界面和业务逻辑等部分。这里给出一个简单的框架,但请注意这只是一个基础示例,实际应用可能需要更复杂的错误处理和安全性考虑。
```python
import sqlite3
from flask import Flask, request, render_template
# 创建Flask应用
app = Flask(__name__)
# 连接到SQLite数据库(如果不存在,会自动创建)
db_path = 'car_rental.db'
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 创建表
cursor.execute("""
CREATE TABLE IF NOT EXISTS rentals (
id INTEGER PRIMARY KEY,
car_id INTEGER,
customer_name TEXT,
rental_date DATE,
return_date DATE
)
""")
# 示例:展示租车列表
@app.route('/rentals')
def show_rentals():
cursor.execute("SELECT * FROM rentals")
rentals = cursor.fetchall()
return render_template('rentals.html', rentals=rentals)
# 示例:添加租车信息
@app.route('/add_rental', methods=['POST'])
def add_rental():
car_id = int(request.form['car_id'])
customer_name = request.form['customer_name']
rental_date = request.form['rental_date']
return_date = request.form['return_date']
cursor.execute(
"INSERT INTO rentals (car_id, customer_name, rental_date, return_date) VALUES (?, ?, ?, ?)",
(car_id, customer_name, rental_date, return_date)
)
conn.commit()
return 'Rent added successfully'
# 启动应用
if __name__ == '__main__':
app.run(debug=True)
阅读全文