108 lines
2.6 KiB
Markdown
108 lines
2.6 KiB
Markdown
|
|
# 数据库初始化说明
|
||
|
|
|
||
|
|
## 📋 概述
|
||
|
|
|
||
|
|
`promptsTemplates.py` 文件已经更新,现在支持初始化本地数据库和腾讯云数据库。
|
||
|
|
|
||
|
|
## 🚀 使用方法
|
||
|
|
|
||
|
|
### 方法一:直接运行脚本
|
||
|
|
|
||
|
|
```bash
|
||
|
|
# 初始化本地数据库(默认)
|
||
|
|
python src/flask_prompt_master/promptsTemplates.py
|
||
|
|
|
||
|
|
# 初始化本地数据库(显式指定)
|
||
|
|
python src/flask_prompt_master/promptsTemplates.py local
|
||
|
|
python src/flask_prompt_master/promptsTemplates.py l
|
||
|
|
|
||
|
|
# 初始化腾讯云数据库
|
||
|
|
python src/flask_prompt_master/promptsTemplates.py tencent
|
||
|
|
python src/flask_prompt_master/promptsTemplates.py t
|
||
|
|
```
|
||
|
|
|
||
|
|
### 方法二:使用交互式工具
|
||
|
|
|
||
|
|
```bash
|
||
|
|
# 运行交互式初始化工具
|
||
|
|
python test_tencent_db_init.py
|
||
|
|
```
|
||
|
|
|
||
|
|
### 方法三:在代码中调用
|
||
|
|
|
||
|
|
```python
|
||
|
|
from src.flask_prompt_master.promptsTemplates import init_tencent_db, init_local_db
|
||
|
|
|
||
|
|
# 初始化腾讯云数据库
|
||
|
|
init_tencent_db()
|
||
|
|
|
||
|
|
# 初始化本地数据库
|
||
|
|
init_local_db()
|
||
|
|
```
|
||
|
|
|
||
|
|
## 🔧 数据库配置
|
||
|
|
|
||
|
|
### 本地数据库配置
|
||
|
|
- **主机**: localhost:3306
|
||
|
|
- **用户**: root
|
||
|
|
- **密码**: 123456
|
||
|
|
- **数据库**: pro_db
|
||
|
|
|
||
|
|
### 腾讯云数据库配置
|
||
|
|
- **主机**: gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com:24936
|
||
|
|
- **用户**: root
|
||
|
|
- **密码**: !Rjb12191
|
||
|
|
- **数据库**: pro_db
|
||
|
|
|
||
|
|
## 📊 功能特性
|
||
|
|
|
||
|
|
1. **智能检测**: 自动检测是否已有模板数据,避免重复插入
|
||
|
|
2. **错误处理**: 完善的异常处理和回滚机制
|
||
|
|
3. **详细日志**: 提供详细的执行过程信息
|
||
|
|
4. **灵活配置**: 支持多种数据库类型
|
||
|
|
5. **安全操作**: 使用事务确保数据一致性
|
||
|
|
|
||
|
|
## ⚠️ 注意事项
|
||
|
|
|
||
|
|
1. **网络连接**: 确保能够访问腾讯云数据库
|
||
|
|
2. **权限检查**: 确保数据库用户有创建表和插入数据的权限
|
||
|
|
3. **数据备份**: 建议在初始化前备份现有数据
|
||
|
|
4. **防火墙**: 确保腾讯云数据库的防火墙规则允许连接
|
||
|
|
|
||
|
|
## 🔍 故障排除
|
||
|
|
|
||
|
|
### 连接失败
|
||
|
|
- 检查网络连接
|
||
|
|
- 验证数据库配置信息
|
||
|
|
- 确认防火墙设置
|
||
|
|
|
||
|
|
### 权限错误
|
||
|
|
- 检查数据库用户权限
|
||
|
|
- 确认数据库名称正确
|
||
|
|
|
||
|
|
### 数据插入失败
|
||
|
|
- 查看详细错误信息
|
||
|
|
- 检查模板数据格式
|
||
|
|
- 验证表结构
|
||
|
|
|
||
|
|
## 📝 执行示例
|
||
|
|
|
||
|
|
```bash
|
||
|
|
$ python src/flask_prompt_master/promptsTemplates.py tencent
|
||
|
|
|
||
|
|
🚀 开始初始化腾讯云数据库...
|
||
|
|
🔗 连接到腾讯云数据库...
|
||
|
|
✅ prompt_template 表创建/检查完成
|
||
|
|
📝 开始插入模板数据...
|
||
|
|
✅ 成功插入 150 个模板数据!
|
||
|
|
🎉 数据库初始化完成!
|
||
|
|
```
|
||
|
|
|
||
|
|
## 🎯 下一步
|
||
|
|
|
||
|
|
初始化完成后,您可以:
|
||
|
|
1. 验证数据是否正确插入
|
||
|
|
2. 测试应用程序连接
|
||
|
|
3. 配置生产环境
|
||
|
|
4. 设置定期备份
|