chore: 批量更新脚本与数据处理优化

1. 调整database.py批量处理记录数为400
2. 新增convert_available.py用于将available字段的JSON数组格式转换为逗号分隔字符串
3. 更新test.py为数据库查询导出脚本
4. 生成并更新SQL更新语句文件
5. 修正jupyter notebook中的数据库连接配置与代码逻辑
This commit is contained in:
2026-06-16 23:58:44 +08:00
parent 9fb191884e
commit 411b96a60c
8 changed files with 831 additions and 2547 deletions
+34 -129
View File
@@ -1,131 +1,36 @@
import requests
import pymysql
import json
# json
import random
# 创建数据库连接
connection = pymysql.connect(
host='11.1.1.5',
user='root',
password='@HXYD1109mysql',
database='school_server',
charset='utf8'
)
# 创建游标对象
cursor = connection.cursor()
sql="select * from temp_schoolwork_plan"
cursor.execute(sql)
result=cursor.fetchall()
def create_table(cursor,result):
data = []
for i in result:
list_list = list(i)
des=cursor.description # 获取表详情,字段名,长度,属性等
t = ",".join([item[0] for item in des])
table_head = t.split(',') # # 查询表列名 用,分割
dict_result = dict(zip(table_head, list_list)) # 打包为元组的列表 再转换为字典
data.append(dict_result) # 将字典添加到list_result中
return data
table = create_table(cursor,result)
"""
请求 Dify API 的示例脚本
接口: POST http://net2.hxzhxy.cn:5093/v1/chat-messages
使用方法:
1. 设置环境变量 DIFY_API_KEY,或者直接在下面填入 api_key
2. 运行脚本
"""
def send_chat_message(
query: str,
api_key: str = None,
api_url: str = "http://net2.hxzhxy.cn:5093/v1/chat-messages",
user: str = "abc-123",
response_mode: str = "streaming", # streaming 或 blocking
conversation_id: str = "",
inputs: dict = None,
files: list = None
):
"""发送对话消息到 Dify API"""
if api_key is None:
import os
api_key = os.environ.get("DIFY_API_KEY", "")
if inputs is None:
inputs = {}
if files is None:
files = []
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"inputs": inputs,
"query": query,
"response_mode": response_mode,
"conversation_id": conversation_id,
"user": user,
"files": files
}
print(f"请求地址: {api_url}")
print(f"用户: {user}")
print(f"消息: {query}")
print(f"响应模式: {response_mode}")
if response_mode == "streaming":
# 流式响应
response = requests.post(
url=api_url,
headers=headers,
json=payload,
stream=True,
timeout=120
)
if response.status_code != 200:
print(f"请求失败: {response.status_code}")
print(response.text)
return
print("\n--- 流式响应 ---")
for line in response.iter_lines():
if line:
try:
data = json.loads(line.decode("utf-8").lstrip("data: "))
if "answer" in data:
print(data["answer"], end="", flush=True)
except (json.JSONDecodeError, KeyError):
pass
print("\n--- 响应结束 ---")
return response
else:
# 非流式响应(blocking
response = requests.post(
url=api_url,
headers=headers,
json=payload,
timeout=120
)
if response.status_code != 200:
print(f"请求失败: {response.status_code}")
print(response.text)
return response
result = response.json()
print(f"\n对话ID: {result.get('conversation_id', '')}")
print(f"回复: {result.get('answer', '')}")
return result
if __name__ == "__main__":
# ===== 配置参数(请在运行前填写)=====
API_KEY = "app-T0kpinzer52gZDGCtipsShVm" # Dify API Key
USER_ID = "xtlft@qq.com"
test = {
"id": 293,
"batch_name": "英语期末模拟",
"fixed_header": "[\"姓名\", \"学号\", \"听力\", \"写作\", \"答题卡\", \"总分\"]",
"variable_header": "[\"姓名\", \"学号\", \"听力\", \"写作\", \"答题卡\", \"总分\"]",
"team_id": 19,
"create_time": "2026-01-12 10:19:43",
"update_time": "2026-06-09 11:32:44",
"operator": "handongmei",
"tea_id": 743,
"tea_name": "韩冬梅",
"state": 1,
"semester": None,
"subject": "英语",
"exam_time": None,
"grade_id": None,
"batch_no": None
},
# 示例1: 流式对话
send_chat_message(
query=json.dumps(test, ensure_ascii=False),
api_key=API_KEY,
user=USER_ID,
response_mode="streaming"
)
for item in table:
item["create_time"]=''
item["update_time"]=''
with open("table.json","w") as f:
f.write(json.dumps(table,ensure_ascii=False,indent=4))