生成提示词模板
This commit is contained in:
23
test_db.py
23
test_db.py
@@ -1,10 +1,15 @@
|
||||
from flask_prompt_master import create_app, db
|
||||
import pymysql
|
||||
from flask_prompt_master.init_db import templates
|
||||
from flask_prompt_master.init_db import templates as init_templates
|
||||
from flask_prompt_master.templates.prompts import templates as prompt_templates
|
||||
|
||||
def insert_all_templates():
|
||||
"""向 prompt_template 表插入所有模板数据"""
|
||||
try:
|
||||
# 合并两个模板列表
|
||||
all_templates = init_templates + prompt_templates
|
||||
print(f"准备插入的模板总数: {len(all_templates)}")
|
||||
|
||||
# 连接MySQL数据库
|
||||
conn = pymysql.connect(
|
||||
host='localhost',
|
||||
@@ -30,7 +35,9 @@ def insert_all_templates():
|
||||
# 遍历所有模板数据并插入
|
||||
success_count = 0
|
||||
duplicate_count = 0
|
||||
for template in templates:
|
||||
error_count = 0
|
||||
|
||||
for template in all_templates:
|
||||
try:
|
||||
# 检查模板名称是否已存在
|
||||
cursor.execute(check_sql, (template['name'],))
|
||||
@@ -59,16 +66,20 @@ def insert_all_templates():
|
||||
print(f"成功插入模板: {template['name']}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"插入模板 {template['name']} 失败: {str(e)}")
|
||||
print(f"插入模板 {template['name']} 失败:")
|
||||
print(f"错误类型: {type(e).__name__}")
|
||||
print(f"错误信息: {str(e)}")
|
||||
error_count += 1
|
||||
continue
|
||||
|
||||
# 提交事务
|
||||
conn.commit()
|
||||
|
||||
print("\n=== 数据插入完成 ===")
|
||||
print(f"成功插入 {success_count} 个模板")
|
||||
print(f"跳过重复 {duplicate_count} 个模板")
|
||||
print(f"总计 {len(templates)} 个模板")
|
||||
print(f"成功插入: {success_count} 个模板")
|
||||
print(f"重复跳过: {duplicate_count} 个模板")
|
||||
print(f"插入失败: {error_count} 个模板")
|
||||
print(f"总计模板: {len(all_templates)} 个")
|
||||
print("===================")
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user