zmk
2024-08-28 1ae38ab34bcbdff622c7623119ee54bad419f4ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
# 导入Flask类
from flask import Flask
from flask import jsonify
from flask import request
from flask_cors import CORS
import pymssql
 
 
# Flask函数接收一个参数__name__,它会指向程序所在的包
app = Flask(__name__)
CORS(app, supports_credentials=True, resources=r'/*')
 
server = '192.168.0.123:1433'
user='sa'
password='admin123X'
database ='microseism3'
 
#根据月份获取数据
def get_event_location_data(month):
     
    conn = pymssql.connect(server=server, user=user, password=password, database=database,as_dict=True)
    cursor = conn.cursor()
    res =[] 
    try:
        sqlStr = 'SELECT * FROM dbo.event_location_'+ str(month)
        # 执行查询语句或其他操作
        cursor.execute(sqlStr)
    
        # 获取结果集
        result = cursor.fetchall()
    
        for row in result:
           dic={"x":row["Event_X"],"y":row["Event_Y"],"z":row["Event_Z"],"v":row["Event_Energy"]}
           res.append(dic)
        
    except Exception as e:
        print("Error occurred:", str(e))
        return []
      
    finally:
        # 关闭连接
        cursor.close()
        conn.close()
    return res   
 
#根据传入的月份查询数据
@app.route('/get_event_location_data', methods=['GET'])
def event_location_data():   
    month = request.args.get('month') 
    res =  get_event_location_data(month)
    return jsonify(res)
 
 
if __name__ == '__main__':
    #app.run()    # 可以指定运行的主机IP地址,端口,是否开启调试模式
    app.run(host="192.168.0.107", port=8080)