用python幫我寫一個設備維修系統
时间: 2023-03-26 12:01:43 浏览: 146
好的,我可以为您提供一个设备维修系统的 Python 代码。首先,您需要安装 Flask 和 SQLite3 库。以下是代码示例:
```python
from flask import Flask, request, render_template
import sqlite3
app = Flask(__name__)
# 创建数据库
conn = sqlite3.connect('devices.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS devices
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT NOT NULL,
status TEXT NOT NULL);''')
conn.commit()
conn.close()
# 主页
@app.route('/')
def index():
conn = sqlite3.connect('devices.db')
c = conn.cursor()
c.execute('SELECT * FROM devices')
devices = c.fetchall()
conn.close()
return render_template('index.html', devices=devices)
# 添加设备
@app.route('/add', methods=['GET', 'POST'])
def add_device():
if request.method == 'POST':
name = request.form['name']
description = request.form['description']
status = request.form['status']
conn = sqlite3.connect('devices.db')
c = conn.cursor()
c.execute('INSERT INTO devices (name, description, status) VALUES (?, ?, ?)',
(name, description, status))
conn.commit()
conn.close()
return '设备添加成功!'
else:
return render_template('add.html')
# 修改设备
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit_device(id):
if request.method == 'POST':
name = request.form['name']
description = request.form['description']
status = request.form['status']
conn = sqlite3.connect('devices.db')
c = conn.cursor()
c.execute('UPDATE devices SET name=?, description=?, status=? WHERE id=?',
(name, description, status, id))
conn.commit()
conn.close()
return '设备修改成功!'
else:
conn = sqlite3.connect('devices.db')
c = conn.cursor()
c.execute('SELECT * FROM devices WHERE id=?', (id,))
device = c.fetchone()
conn.close()
return render_template('edit.html', device=device)
# 删除设备
@app.route('/delete/<int:id>')
def delete_device(id):
conn = sqlite3.connect('devices.db')
c = conn.cursor()
c.execute('DELETE FROM devices WHERE id=?', (id,))
conn.commit()
conn.close()
return '设备删除成功!'
if __name__ == '__main__':
app.run()
```
您可以将上述代码保存为 `app.py` 文件,并在终端中运行 `python app.py` 命令启动应用程序。然后,您可以在浏览器中访问 `http://localhost:500` 查看设备维修系统的主页。
阅读全文