Update .gitignore and add files
This commit is contained in:
34
back/blueprints/shi1.py
Normal file
34
back/blueprints/shi1.py
Normal file
@ -0,0 +1,34 @@
|
||||
from flask import Blueprint, jsonify
|
||||
import sqlite3
|
||||
from flask_cors import cross_origin
|
||||
|
||||
bp = Blueprint('shi1', __name__)
|
||||
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect('agriculture.db')
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
@bp.route('/moisture', methods=['GET'])
|
||||
@cross_origin()
|
||||
def get_moisture_data():
|
||||
conn = get_db_connection()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
# 获取最近7条记录并按日期正序排列
|
||||
cur.execute('''
|
||||
SELECT record_date as date, moisture
|
||||
FROM (
|
||||
SELECT record_date, moisture
|
||||
FROM soil_moisture
|
||||
ORDER BY id DESC
|
||||
LIMIT 7
|
||||
)
|
||||
ORDER BY date ASC
|
||||
''')
|
||||
rows = cur.fetchall()
|
||||
return jsonify([dict(row) for row in rows])
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
finally:
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user