Files
ai_english/data/insert_vocabulary_books.py
2025-11-17 13:39:05 +08:00

166 lines
4.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""插入词汇书数据"""
import mysql.connector
from datetime import datetime
# 数据库配置
db_config = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'JKjk20011115',
'database': 'ai_english_learning',
'charset': 'utf8mb4'
}
# 词汇书数据
vocabulary_books = [
{
'id': 'cet4_core_2500',
'name': '大学英语四级核心词汇',
'description': '涵盖CET-4考试核心词汇2500个',
'category': 'CET-4核心词汇',
'level': 'intermediate',
'total_words': 2500,
'icon': '📚',
'color': '#4CAF50',
'sort_order': 1
},
{
'id': 'cet6_core_3000',
'name': '大学英语六级核心词汇',
'description': '涵盖CET-6考试核心词汇3000个',
'category': 'CET-6核心词汇',
'level': 'advanced',
'total_words': 3000,
'icon': '📖',
'color': '#2196F3',
'sort_order': 2
},
{
'id': 'toefl_high_3500',
'name': '托福高频词汇',
'description': '托福考试高频词汇3500个',
'category': 'TOEFL高频词汇',
'level': 'advanced',
'total_words': 3500,
'icon': '🎓',
'color': '#FF9800',
'sort_order': 3
},
{
'id': 'ielts_high_3500',
'name': '雅思高频词汇',
'description': '雅思考试高频词汇3500个',
'category': 'IELTS高频词汇',
'level': 'advanced',
'total_words': 3500,
'icon': '🌟',
'color': '#9C27B0',
'sort_order': 4
},
{
'id': 'primary_core_1000',
'name': '小学英语核心词汇',
'description': '小学阶段必备核心词汇1000个',
'category': '小学核心词汇',
'level': 'beginner',
'total_words': 1000,
'icon': '🎈',
'color': '#E91E63',
'sort_order': 5
},
{
'id': 'junior_core_1500',
'name': '初中英语核心词汇',
'description': '初中阶段必备核心词汇1500个',
'category': '初中核心词汇',
'level': 'elementary',
'total_words': 1500,
'icon': '📝',
'color': '#00BCD4',
'sort_order': 6
},
{
'id': 'senior_core_3500',
'name': '高中英语核心词汇',
'description': '高中阶段必备核心词汇3500个',
'category': '高中核心词汇',
'level': 'intermediate',
'total_words': 3500,
'icon': '📕',
'color': '#FF5722',
'sort_order': 7
},
{
'id': 'business_core_1000',
'name': '商务英语核心词汇',
'description': '商务场景常用核心词汇1000个',
'category': '商务英语',
'level': 'intermediate',
'total_words': 1000,
'icon': '💼',
'color': '#607D8B',
'sort_order': 8
}
]
def main():
try:
# 连接数据库
conn = mysql.connector.connect(**db_config)
cursor = conn.cursor()
print("⏩ 跳过表创建直接插入数据表应该已由GORM自动创建")
# 插入词汇书数据
insert_sql = """
INSERT INTO `ai_vocabulary_books`
(`id`, `name`, `description`, `category`, `level`, `total_words`, `icon`, `color`, `is_system`, `is_active`, `sort_order`)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, TRUE, TRUE, %s)
ON DUPLICATE KEY UPDATE
`name` = VALUES(`name`),
`description` = VALUES(`description`),
`category` = VALUES(`category`),
`level` = VALUES(`level`),
`total_words` = VALUES(`total_words`),
`icon` = VALUES(`icon`),
`color` = VALUES(`color`),
`sort_order` = VALUES(`sort_order`)
"""
for book in vocabulary_books:
cursor.execute(insert_sql, (
book['id'],
book['name'],
book['description'],
book['category'],
book['level'],
book['total_words'],
book['icon'],
book['color'],
book['sort_order']
))
print(f"✅ 插入词汇书: {book['name']}")
conn.commit()
print(f"\n🎉 成功插入 {len(vocabulary_books)} 个词汇书!")
# 查询验证
cursor.execute("SELECT COUNT(*) FROM ai_vocabulary_books")
count = cursor.fetchone()[0]
print(f"📊 当前数据库中共有 {count} 个词汇书")
except mysql.connector.Error as err:
print(f"❌ 数据库错误: {err}")
finally:
if cursor:
cursor.close()
if conn:
conn.close()
if __name__ == '__main__':
main()