You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8 KiB

from datetime import datetime
from flask import request, jsonify
from flask import *
from flask_sqlalchemy import SQLAlchemy
import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:LH20021212@localhost:3306/智能家居系统'
db = SQLAlchemy(app)
try:
with app.app_context():
db.create_all()
print("数据库连接成功!")
except Exception as e:
print(f"数据库连接失败: {str(e)}")
class JiaJu(db.Model):
_tablename_ = 'JiaJu'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
color = db.Column(db.String(50))
brand = db.Column(db.String(50))
price = db.Column(db.Float, nullable=False)
production_date = db.Column(db.Date, nullable=False)
# #增
# @app.route('/add_jiaju', methods=['POST'])
# def add_jiaju():
# data = request.get_json()
# new_jiaju = JiaJu(
# name=data['name'],
# color=data.get('color'),
# brand=data.get('brand'),
# price=data['price'],
# production_date=datetime.strptime(data['production_date'], '%Y-%m-%d')
# )
# db.session.add(new_jiaju)
# db.session.commit()
# return jsonify({"message": "家居添加成功"}), 201
# #查
# @app.route('/jiaju', methods=['GET'])
# def get_all_jiaju():
# jiaju_list = JiaJu.query.all()
# result = [{"id": item.id, "name": item.name, "color": item.color, "brand": item.brand,
# "price": item.price, "production_date": item.production_date.strftime('%Y-%m-%d')} for item in jiaju_list]
# return jsonify(result)
# #获取
# @app.route('/jiaju/<int:jiaju_id>', methods=['GET'])
# def get_jiaju(jiaju_id):
# jiaju = JiaJu.query.get_or_404(jiaju_id)
# return jsonify({
# "id": jiaju.id,
# "name": jiaju.name,
# "color": jiaju.color,
# "brand": jiaju.brand,
# "price": jiaju.price,
# "production_date": jiaju.production_date.strftime('%Y-%m-%d')
# })
# #更
# @app.route('/jiaju/<int:jiaju_id>', methods=['PUT'])
# def update_jiaju(jiaju_id):
# jiaju = JiaJu.query.get_or_404(jiaju_id)
# data = request.get_json()
# if 'name' in data:
# jiaju.name = data['name']
# if 'color' in data:
# jiaju.color = data['color']
# if 'brand' in data:
# jiaju.brand = data['brand']
# if 'price' in data:
# jiaju.price = data['price']
# if 'production_date' in data:
# jiaju.production_date = datetime.strptime(data['production_date'], '%Y-%m-%d')
# db.session.commit()
# return jsonify({"message": "家居信息更新成功"})
# #删
# @app.route('/jiaju/<int:jiaju_id>', methods=['DELETE'])
# def delete_jiaju(jiaju_id):
# jiaju = JiaJu.query.get_or_404(jiaju_id)
# db.session.delete(jiaju)
# db.session.commit()
# return jsonify({"message": "家居删除成功"})