diff --git a/src/data_storage/mysql_storage.py b/src/data_storage/mysql_storage.py index 92466dc..f065f65 100644 --- a/src/data_storage/mysql_storage.py +++ b/src/data_storage/mysql_storage.py @@ -208,13 +208,17 @@ class MySQLStorage(StorageLayer): # 保存到数据库 engine = self.db_manager._create_db_engine(self.database) - df_to_save.to_sql( - table_name, - engine, - if_exists="append", - index=False, - method="multi", - ) + try: + with engine.begin() as conn: + df_to_save.to_sql( + table_name, + conn, + if_exists="append", + index=False, + method="multi", + ) + finally: + engine.dispose() logger.info(f"Saved {len(df_to_save)} klines: {symbol}_{period}") return len(df_to_save) @@ -252,15 +256,27 @@ class MySQLStorage(StorageLayer): ] df = df[cols] + print(f"[DEBUG] 准备保存 {len(df)} 条记录到 {table_name}") + print( + f"[DEBUG] DataFrame shape: {df.shape}, columns: {df.columns.tolist()}" + ) + print(f"[DEBUG] 前3行:\n{df.head(3)}") + # 保存到数据库 engine = self.db_manager._create_db_engine(self.database) - df.to_sql( - table_name, - engine, - if_exists="append", - index=False, - method="multi", - ) + try: + with engine.begin() as conn: + result = df.to_sql( + table_name, + conn, + if_exists="append", + index=False, + method="multi", + ) + print(f"[DEBUG] to_sql返回值: {result}") + print(f"[DEBUG] 事务已提交") + finally: + engine.dispose() logger.info( f"Saved {len(df)} indicator records: {symbol}_{period}_{indicator_name}" diff --git a/src/database_manager/table_schemas.py b/src/database_manager/table_schemas.py index 9ef298d..8e5a189 100644 --- a/src/database_manager/table_schemas.py +++ b/src/database_manager/table_schemas.py @@ -243,19 +243,19 @@ def create_indicator_table_definition(indicator: str, period: str) -> Dict[str, indicator_columns = {} if indicator == "macd": indicator_columns = { - "macd_dif": {"type": "Float", "nullable": False, "comment": "DIF值"}, - "macd_dea": {"type": "Float", "nullable": False, "comment": "DEA值"}, + "macd_dif": {"type": "Float", "nullable": True, "comment": "DIF值"}, + "macd_dea": {"type": "Float", "nullable": True, "comment": "DEA值"}, "macd_histogram": { "type": "Float", - "nullable": False, + "nullable": True, "comment": "MACD柱值", }, } elif indicator == "kdj": indicator_columns = { - "kdj_k": {"type": "Float", "nullable": False, "comment": "K值"}, - "kdj_d": {"type": "Float", "nullable": False, "comment": "D值"}, - "kdj_j": {"type": "Float", "nullable": False, "comment": "J值"}, + "kdj_k": {"type": "Float", "nullable": True, "comment": "K值"}, + "kdj_d": {"type": "Float", "nullable": True, "comment": "D值"}, + "kdj_j": {"type": "Float", "nullable": True, "comment": "J值"}, } elif indicator == "rsi": indicator_columns = { @@ -265,9 +265,9 @@ def create_indicator_table_definition(indicator: str, period: str) -> Dict[str, } elif indicator == "boll": indicator_columns = { - "boll_upper": {"type": "Float", "nullable": False, "comment": "上轨"}, - "boll_mid": {"type": "Float", "nullable": False, "comment": "中轨"}, - "boll_lower": {"type": "Float", "nullable": False, "comment": "下轨"}, + "boll_upper": {"type": "Float", "nullable": True, "comment": "上轨"}, + "boll_mid": {"type": "Float", "nullable": True, "comment": "中轨"}, + "boll_lower": {"type": "Float", "nullable": True, "comment": "下轨"}, "boll_width": {"type": "Float", "nullable": True, "comment": "带宽"}, } elif indicator in ["ma", "ema"]: diff --git a/src/database_manager/table_utils.py b/src/database_manager/table_utils.py index c599101..0851fad 100644 --- a/src/database_manager/table_utils.py +++ b/src/database_manager/table_utils.py @@ -40,15 +40,15 @@ def get_indicator_table_name(indicator: str, period: str) -> str: period: 周期(1m/5m/60m/1d等) Returns: - 表名(如 macd_1m) + 表名(如 indicators_1m_macd) Examples: >>> get_indicator_table_name("macd", "1m") - 'macd_1m' + 'indicators_1m_macd' >>> get_indicator_table_name("kdj", "1d") - 'kdj_1d' + 'indicators_1d_kdj' """ - return f"{indicator}_{period}" + return f"indicators_{period}_{indicator}" def get_all_kline_table_names() -> List[str]: