Files
aitsc/docs/development/数据库操作.txt

165 lines
5.1 KiB
Plaintext

c
from flask_prompt_master import create_app, db
import pymysql
def insert_data():
"""向 container_orchestration_assistant 表插入数据"""
try:
# 连接MySQL数据库
conn = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='food_db',
charset='utf8mb4'
)
cursor = conn.cursor()
# 准备插入的数据
template_data = {
'name': '容器编排助手',
'description': '优化容器化、自动化部署相关的提示词',
'category': '架构设计',
'industry': '技术研发',
'profession': '高级工程师',
'sub_category': '容器编排',
'system_prompt': """你是一个专业的容器编排提示词专家。对于容器化需求,你需要:
1. 明确容器化策略
- 确定容器化范围
- 选择基础镜像
- 规划镜像分层
- 优化构建过程
2. 设计编排配置
- 定义服务组件
- 配置资源限制
- 设置环境变量
- 管理配置文件
3. 规划资源调度
- 节点亲和性策略
- 容器分布规则
- 资源预留配置
- 弹性伸缩方案
4. 考虑服务发现
- 服务注册机制
- 负载均衡策略
- 健康检查配置
- 网络通信规则
5. 注意状态管理
- 持久化存储方案
- 数据备份策略
- 日志收集管理
- 监控告警集成"""
}
# 构建 SQL 插入语句
sql = """
INSERT INTO container_orchestration_assistant
(name, description, category, industry, profession, sub_category, system_prompt)
VALUES (%(name)s, %(description)s, %(category)s, %(industry)s, %(profession)s,
%(sub_category)s, %(system_prompt)s)
"""
# 执行插入
cursor.execute(sql, template_data)
# 提交事务
conn.commit()
print("=== 数据插入成功 ===")
print(f"名称: {template_data['name']}")
print(f"描述: {template_data['description']}")
print(f"分类: {template_data['category']}")
print(f"行业: {template_data['industry']}")
print(f"职业: {template_data['profession']}")
print(f"子类别: {template_data['sub_category']}")
print("===================")
except Exception as e:
print(f"数据库操作失败: {str(e)}")
conn.rollback()
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
insert_data()
#插入全部数据
from flask_prompt_master import create_app, db
import pymysql
from flask_prompt_master.init_db import templates
def insert_all_templates():
"""向 container_orchestration_assistant 表插入所有模板数据"""
try:
# 连接MySQL数据库
conn = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='food_db',
charset='utf8mb4'
)
cursor = conn.cursor()
# 先清空表
cursor.execute("TRUNCATE TABLE container_orchestration_assistant")
# SQL 插入语句
sql = """
INSERT INTO container_orchestration_assistant
(name, description, category, industry, profession, sub_category, system_prompt)
VALUES (%(name)s, %(description)s, %(category)s, %(industry)s, %(profession)s,
%(sub_category)s, %(system_prompt)s)
"""
# 遍历所有模板数据并插入
success_count = 0
for template in templates:
try:
# 准备模板数据
template_data = {
'name': template['name'],
'description': template['description'],
'category': template.get('category', ''),
'industry': template.get('industry', ''),
'profession': template.get('profession', ''),
'sub_category': template.get('sub_category', ''),
'system_prompt': template['system_prompt']
}
# 执行插入
cursor.execute(sql, template_data)
success_count += 1
print(f"成功插入模板: {template['name']}")
except Exception as e:
print(f"插入模板 {template['name']} 失败: {str(e)}")
continue
# 提交事务
conn.commit()
print("\n=== 数据插入完成 ===")
print(f"成功插入 {success_count} 个模板")
print(f"总计 {len(templates)} 个模板")
print("===================")
except Exception as e:
print(f"数据库连接失败: {str(e)}")
if 'conn' in locals():
conn.rollback()
finally:
if 'cursor' in locals():
cursor.close()
if 'conn' in locals():
conn.close()
if __name__ == '__main__':
insert_all_templates()