✨ feat(database): 添加表存在性检查功能,优化数据库操作前的验证流程
This commit is contained in:
parent
38d1fb37ce
commit
ce86f2ae4f
@ -1,6 +1,8 @@
|
||||
import os
|
||||
|
||||
import pandas as pd
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy import create_engine, text, inspect
|
||||
|
||||
from utils import load_config
|
||||
|
||||
|
||||
@ -29,6 +31,21 @@ class DatabaseManager:
|
||||
"""根据表键名获取实际表名"""
|
||||
return self.config['sqlite']['table_name'].get(key, key)
|
||||
|
||||
def table_exists(self, table_key):
|
||||
"""
|
||||
检查表是否存在
|
||||
|
||||
参数:
|
||||
table_key (str): 数据表键名
|
||||
|
||||
返回:
|
||||
bool: 表是否存在
|
||||
"""
|
||||
table_name = self.get_table_name(table_key)
|
||||
engine = self.get_engine()
|
||||
inspector = inspect(engine)
|
||||
return inspector.has_table(table_name)
|
||||
|
||||
def get_existing_trade_dates(self, table_key):
|
||||
"""
|
||||
从数据库中获取已有的交易日期
|
||||
@ -39,6 +56,11 @@ class DatabaseManager:
|
||||
返回:
|
||||
set: 已存在于数据库中的交易日期集合
|
||||
"""
|
||||
# 先检查表是否存在
|
||||
if not self.table_exists(table_key):
|
||||
print(f"表 '{self.get_table_name(table_key)}' 不存在,返回空集合")
|
||||
return set()
|
||||
|
||||
table_name = self.get_table_name(table_key)
|
||||
engine = self.get_engine()
|
||||
query = f"SELECT DISTINCT trade_date FROM {table_name}"
|
||||
|
Loading…
Reference in New Issue
Block a user