migrate: last 4 JSON files — live_prices, market, mtf_cache, capital_flow → DB
This commit is contained in:
+67
@@ -387,6 +387,28 @@ def init_all_tables(conn: sqlite3.Connection):
|
||||
last_scanned_at TEXT,
|
||||
found_count INTEGER DEFAULT 0
|
||||
);
|
||||
|
||||
-- 实时价格快照(替代 live_prices.json)
|
||||
CREATE TABLE IF NOT EXISTS live_prices (
|
||||
code TEXT PRIMARY KEY,
|
||||
price REAL,
|
||||
change_pct REAL,
|
||||
updated_at TEXT DEFAULT (datetime('now','localtime'))
|
||||
);
|
||||
|
||||
-- 多周期缓存(替代 multi_tf_cache.json)
|
||||
CREATE TABLE IF NOT EXISTS mtf_cache (
|
||||
code TEXT PRIMARY KEY,
|
||||
cache_json TEXT,
|
||||
updated_at TEXT DEFAULT (datetime('now','localtime'))
|
||||
);
|
||||
|
||||
-- 资金流缓存(替代 capital_flow_cache.json)
|
||||
CREATE TABLE IF NOT EXISTS capital_flow_cache (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cache_json TEXT,
|
||||
updated_at TEXT DEFAULT (datetime('now','localtime'))
|
||||
);
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
@@ -1120,3 +1142,48 @@ def query_cash_log(conn, limit: int = 20) -> list[dict]:
|
||||
"SELECT * FROM cash_log ORDER BY id DESC LIMIT ?", (limit,)
|
||||
).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
# ═══ live_prices / mtf_cache / capital_flow_cache 写函数 ═══
|
||||
|
||||
def write_live_prices(conn, prices: dict):
|
||||
"""写入实时价格快照(替代 live_prices.json)"""
|
||||
import json
|
||||
for code, info in prices.items():
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO live_prices (code, price, change_pct, updated_at) VALUES (?,?,?,datetime('now','localtime'))",
|
||||
(code, info.get('price'), info.get('change_pct'))
|
||||
)
|
||||
|
||||
def read_live_prices(conn) -> dict:
|
||||
rows = conn.execute("SELECT code, price, change_pct FROM live_prices").fetchall()
|
||||
return {r['code']: {'price': r['price'], 'change_pct': r['change_pct']} for r in rows}
|
||||
|
||||
|
||||
def write_mtf_cache(conn, code: str, data: dict):
|
||||
"""写入多周期缓存(替代 multi_tf_cache.json 单条)"""
|
||||
import json
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO mtf_cache (code, cache_json, updated_at) VALUES (?,?,datetime('now','localtime'))",
|
||||
(code, json.dumps(data, ensure_ascii=False))
|
||||
)
|
||||
|
||||
def read_mtf_cache(conn, code: str) -> dict:
|
||||
import json
|
||||
r = conn.execute("SELECT cache_json FROM mtf_cache WHERE code=?", (code,)).fetchone()
|
||||
return json.loads(r['cache_json']) if r else {}
|
||||
|
||||
|
||||
def write_capital_flow_cache(conn, data: dict):
|
||||
"""写入资金流缓存(替代 capital_flow_cache.json)"""
|
||||
import json
|
||||
conn.execute("DELETE FROM capital_flow_cache")
|
||||
conn.execute(
|
||||
"INSERT INTO capital_flow_cache (cache_json, updated_at) VALUES (?,datetime('now','localtime'))",
|
||||
(json.dumps(data, ensure_ascii=False),)
|
||||
)
|
||||
|
||||
def read_capital_flow_cache(conn) -> dict:
|
||||
import json
|
||||
r = conn.execute("SELECT cache_json FROM capital_flow_cache ORDER BY id DESC LIMIT 1").fetchone()
|
||||
return json.loads(r['cache_json']) if r else {}
|
||||
|
||||
Reference in New Issue
Block a user