ti
This commit is contained in:
22
test_db.py
22
test_db.py
@@ -3,7 +3,7 @@ import pymysql
|
||||
from flask_prompt_master.init_db import templates
|
||||
|
||||
def insert_all_templates():
|
||||
"""向 container_orchestration_assistant 表插入所有模板数据"""
|
||||
"""向 prompt_template 表插入所有模板数据"""
|
||||
try:
|
||||
# 连接MySQL数据库
|
||||
conn = pymysql.connect(
|
||||
@@ -16,11 +16,12 @@ def insert_all_templates():
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 先清空表
|
||||
cursor.execute("TRUNCATE TABLE container_orchestration_assistant")
|
||||
cursor.execute("TRUNCATE TABLE prompt_template")
|
||||
|
||||
# SQL 插入语句
|
||||
sql = """
|
||||
INSERT INTO container_orchestration_assistant
|
||||
check_sql = "SELECT COUNT(*) FROM prompt_template WHERE name = %s"
|
||||
insert_sql = """
|
||||
INSERT INTO prompt_template
|
||||
(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)
|
||||
@@ -28,8 +29,18 @@ def insert_all_templates():
|
||||
|
||||
# 遍历所有模板数据并插入
|
||||
success_count = 0
|
||||
duplicate_count = 0
|
||||
for template in templates:
|
||||
try:
|
||||
# 检查模板名称是否已存在
|
||||
cursor.execute(check_sql, (template['name'],))
|
||||
exists = cursor.fetchone()[0] > 0
|
||||
|
||||
if exists:
|
||||
print(f"模板已存在,跳过: {template['name']}")
|
||||
duplicate_count += 1
|
||||
continue
|
||||
|
||||
# 准备模板数据
|
||||
template_data = {
|
||||
'name': template['name'],
|
||||
@@ -42,7 +53,7 @@ def insert_all_templates():
|
||||
}
|
||||
|
||||
# 执行插入
|
||||
cursor.execute(sql, template_data)
|
||||
cursor.execute(insert_sql, template_data)
|
||||
success_count += 1
|
||||
|
||||
print(f"成功插入模板: {template['name']}")
|
||||
@@ -56,6 +67,7 @@ def insert_all_templates():
|
||||
|
||||
print("\n=== 数据插入完成 ===")
|
||||
print(f"成功插入 {success_count} 个模板")
|
||||
print(f"跳过重复 {duplicate_count} 个模板")
|
||||
print(f"总计 {len(templates)} 个模板")
|
||||
print("===================")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user