| 123456789101112131415161718192021222324252627282930313233343536 |
- import pymysql
- import yaml
- import os
- # 切换到脚本所在目录
- script_dir = os.path.dirname(os.path.abspath(__file__))
- os.chdir(script_dir)
- # 读取配置
- with open('config.yaml', 'r', encoding='utf-8') as f:
- config = yaml.safe_load(f)
- db_config = config['database']
- # 连接数据库
- connection = pymysql.connect(
- host=db_config['host'],
- port=db_config['port'],
- user=db_config['user'],
- password=db_config['password'],
- database=db_config['database']
- )
- try:
- with connection.cursor() as cursor:
- # 执行迁移
- sql = "ALTER TABLE tracking_record MODIFY user_id VARCHAR(50)"
- print(f"执行 SQL: {sql}")
- cursor.execute(sql)
- connection.commit()
- print("✓ 数据库迁移成功!tracking_record.user_id 字段已改为 VARCHAR(50)")
- except Exception as e:
- print(f"✗ 迁移失败: {e}")
- connection.rollback()
- finally:
- connection.close()
|