初始化数据库连接
(self)
| 19 | """MySQL 数据库管理类""" |
| 20 | |
| 21 | def __init__(self): |
| 22 | """初始化数据库连接""" |
| 23 | import os |
| 24 | |
| 25 | # 从环境变量读取配置(推荐)或使用默认值 |
| 26 | self.config = { |
| 27 | 'host': os.getenv('MYSQL_HOST', 'localhost'), |
| 28 | 'port': int(os.getenv('MYSQL_PORT', 3306)), |
| 29 | 'user': os.getenv('MYSQL_USER', 'tgbot_user'), |
| 30 | 'password': os.getenv('MYSQL_PASSWORD', 'your_password_here'), |
| 31 | 'database': os.getenv('MYSQL_DATABASE', 'tgbot_verify'), |
| 32 | 'charset': 'utf8mb4', |
| 33 | 'autocommit': False, |
| 34 | } |
| 35 | logger.info(f"MySQL 数据库初始化: {self.config['user']}@{self.config['host']}/{self.config['database']}") |
| 36 | self.init_database() |
| 37 | |
| 38 | def get_connection(self): |
| 39 | """获取数据库连接""" |
nothing calls this directly
no test coverage detected