fix entity recognition company mapping initialization with error handling

This commit is contained in:
sam 2025-10-07 22:29:13 +08:00
parent 894ed3fd0d
commit 37aaeb92f6

View File

@ -2,8 +2,13 @@
from __future__ import annotations
import re
import sqlite3
from typing import Dict, List, Optional, Set, Tuple
from app.utils.logging import get_logger
LOGGER = get_logger(__name__)
# 股票代码正则表达式
A_SH_CODE_PATTERN = re.compile(r"\b(\d{6})(\.(?:SH|SZ))?\b", re.IGNORECASE)
HK_CODE_PATTERN = re.compile(r"\b(\d{4})\.HK\b", re.IGNORECASE)
@ -132,11 +137,22 @@ def initialize_company_mapping(db_connection) -> None:
db_connection: SQLite数据库连接
"""
cursor = db_connection.cursor()
cursor.execute("""
SELECT ts_code, name, short_name
FROM stock_company
WHERE name IS NOT NULL
""")
try:
cursor.execute(
"""
SELECT ts_code, name, short_name
FROM stock_company
WHERE name IS NOT NULL
"""
)
except sqlite3.OperationalError as exc: # pragma: no cover - defensive
LOGGER.debug(
"stock_company 表不存在,跳过公司映射初始化 err=%s",
exc,
extra={"stage": "entity_recognition"},
)
cursor.close()
return
for ts_code, name, short_name in cursor.fetchall():
if name and short_name: