-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
76 lines (63 loc) · 1.81 KB
/
app.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, request
from flask_cors import CORS
import pymysql
import threading
app = Flask(__name__)
CORS(app)
# 数据库连接配置
connection = pymysql.connect(
host='rm-cn-x0r3j6fcx0024svo.rwlb.rds.aliyuncs.com',
user='webs',
password='WebGIS2023',
database='ecnu'
)
lock = threading.Lock() # 创建全局互斥锁
@app.route('/query', methods=['GET'])
def query():
try:
# 创建游标对象
cursor = connection.cursor()
# 获取查询参数
sql = request.args.get('sql')
# 互斥锁
lock.acquire()
# 执行查询
cursor.execute(sql)
# 解锁
lock.release()
# 获取查询结果
results = cursor.fetchall()
# 关闭游标
cursor.close()
# 返回查询结果
return jsonify(results)
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/insert', methods=['POST'])
def add_data():
try:
# 获取POST请求中的JSON数据
data = request.get_json()
sql = data.get('sql')
if sql:
# 创建游标对象
cursor = connection.cursor()
# 互斥锁
lock.acquire()
# 执行插入操作
cursor.execute(sql)
# 提交事务
connection.commit()
# 解锁
lock.release()
# 关闭游标
cursor.close()
# 返回成功响应
return jsonify({'message': 'Data added successfully!'}), 200
else:
return jsonify({'error': 'No SQL code provided.'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host="127.0.0.1", port=5000, debug=True)