You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
979 B

"""初始化 MySQL 数据库"""
import sys
import pymysql
# 先创建数据库(如果不存在)
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='',
charset='utf8mb4'
)
try:
with conn.cursor() as cursor:
cursor.execute("CREATE DATABASE IF NOT EXISTS marketdata CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
print("[OK] 数据库 'marketdata' 创建成功或已存在")
conn.commit()
finally:
conn.close()
# 使用 SQLAlchemy 创建表
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.repositories.database import init_db, engine
from sqlalchemy import inspect
print("\n正在创建数据表...")
init_db()
# 验证表是否创建成功
inspector = inspect(engine)
tables = inspector.get_table_names()
print(f"\n[OK] 已创建 {len(tables)} 个表:")
for table in sorted(tables):
print(f" - {table}")
print("\n[OK] MySQL 数据库初始化完成!")