优化数据
This commit is contained in:
134
force_insert_templates.py
Normal file
134
force_insert_templates.py
Normal file
@@ -0,0 +1,134 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
强制插入所有模板数据到腾讯云数据库
|
||||
"""
|
||||
import pymysql
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加项目根目录到Python路径
|
||||
project_root = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.append(project_root)
|
||||
|
||||
def get_templates():
|
||||
"""获取模板数据"""
|
||||
try:
|
||||
from src.flask_prompt_master.promptsTemplates import templates
|
||||
return templates
|
||||
except ImportError:
|
||||
print("❌ 无法导入模板数据,请确保在项目根目录运行此脚本")
|
||||
return []
|
||||
|
||||
def force_insert_templates():
|
||||
"""强制插入所有模板数据到腾讯云数据库"""
|
||||
print("🚀 开始强制插入所有模板数据到腾讯云数据库...")
|
||||
|
||||
# 腾讯云数据库配置
|
||||
config = {
|
||||
'host': 'gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com',
|
||||
'port': 24936,
|
||||
'user': 'root',
|
||||
'password': '!Rjb12191',
|
||||
'database': 'pro_db',
|
||||
'charset': 'utf8mb4'
|
||||
}
|
||||
|
||||
try:
|
||||
# 连接数据库
|
||||
print("🔗 连接到腾讯云数据库...")
|
||||
conn = pymysql.connect(**config)
|
||||
cursor = conn.cursor()
|
||||
print("✅ 数据库连接成功")
|
||||
|
||||
# 获取模板数据
|
||||
templates = get_templates()
|
||||
if not templates:
|
||||
print("❌ 无法获取模板数据,退出")
|
||||
return
|
||||
|
||||
print(f"📊 准备插入 {len(templates)} 个模板...")
|
||||
|
||||
# 清空现有数据
|
||||
print("🗑️ 清空现有模板数据...")
|
||||
cursor.execute("TRUNCATE TABLE prompt_template")
|
||||
print("✅ 现有数据已清空")
|
||||
|
||||
# 插入所有模板数据
|
||||
print("📝 开始插入所有模板数据...")
|
||||
sql = """
|
||||
INSERT INTO prompt_template
|
||||
(name, description, category, industry, profession, sub_category, system_prompt, is_default)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
|
||||
success_count = 0
|
||||
error_count = 0
|
||||
|
||||
for i, template in enumerate(templates, 1):
|
||||
try:
|
||||
cursor.execute(sql, (
|
||||
template['name'],
|
||||
template['description'],
|
||||
template.get('category', ''),
|
||||
template.get('industry', ''),
|
||||
template.get('profession', ''),
|
||||
template.get('sub_category', ''),
|
||||
template['system_prompt'],
|
||||
template.get('is_default', False)
|
||||
))
|
||||
success_count += 1
|
||||
|
||||
# 每插入50个模板显示一次进度
|
||||
if i % 50 == 0:
|
||||
print(f"📈 已插入 {i}/{len(templates)} 个模板...")
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ 插入模板 '{template['name']}' 失败: {str(e)}")
|
||||
error_count += 1
|
||||
|
||||
# 提交事务
|
||||
conn.commit()
|
||||
|
||||
print("\n" + "="*50)
|
||||
print("🎉 模板数据插入完成!")
|
||||
print(f"✅ 成功插入: {success_count} 个模板")
|
||||
if error_count > 0:
|
||||
print(f"⚠️ 插入失败: {error_count} 个模板")
|
||||
print(f"📊 总计模板: {len(templates)} 个")
|
||||
print("="*50)
|
||||
|
||||
# 验证插入结果
|
||||
cursor.execute("SELECT COUNT(*) FROM prompt_template")
|
||||
final_count = cursor.fetchone()[0]
|
||||
print(f"🔍 数据库中的模板总数: {final_count}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 插入模板数据失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
if 'conn' in locals():
|
||||
conn.rollback()
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
if 'conn' in locals():
|
||||
conn.close()
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("=" * 60)
|
||||
print("🔄 强制插入模板数据工具")
|
||||
print("=" * 60)
|
||||
print("⚠️ 警告:此操作将清空现有的所有模板数据!")
|
||||
|
||||
# 确认操作
|
||||
confirm = input("\n是否继续?(y/N): ").strip().lower()
|
||||
if confirm not in ['y', 'yes', '是']:
|
||||
print("❌ 操作已取消")
|
||||
return
|
||||
|
||||
force_insert_templates()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user