crypto_quant/core/db_manager.py

56 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
from sqlalchemy import create_engine, exc, text
import logging
def save_market_data_to_mysql(df: pd.DataFrame, db_url: str):
"""
将K线行情数据保存到MySQL的crypto_market_data表
:param df: K线数据DataFrame
:param symbol: 交易对
:param bar: K线周期
:param db_url: 数据库连接URL
"""
if df is None or df.empty:
logging.warning("DataFrame为空无需写入数据库。")
return
# 按表字段顺序排列
columns = [
'symbol', 'bar', 'timestamp', 'date_time', 'open', 'high', 'low', 'close',
'volume', 'volCcy', 'volCCyQuote'
]
df = df[columns]
# 建立数据库连接
try:
engine = create_engine(db_url)
try:
df.to_sql(
name='crypto_market_data',
con=engine,
if_exists='append',
index=False,
method='multi'
)
logging.info("数据已成功写入数据库。")
except Exception as e:
logging.error(f'插入数据出错: {e}')
with engine.begin() as conn:
for _, row in df.iterrows():
try:
sql = text("""
INSERT INTO crypto_market_data
(symbol, bar, timestamp, date_time, open, high, low, close, volume, volCcy, volCCyQuote)
VALUES (:symbol, :bar, :timestamp, :date_time, :open, :high, :low, :close, :volume, :volCcy, :volCCyQuote)
ON DUPLICATE KEY UPDATE
open=VALUES(open), high=VALUES(high), low=VALUES(low), close=VALUES(close),
volume=VALUES(volume), volCcy=VALUES(volCcy), volCCyQuote=VALUES(volCCyQuote), date_time=VALUES(date_time)
""")
conn.execute(sql, row.to_dict())
except exc.IntegrityError as e:
logging.error(f'唯一索引冲突: {e}')
except Exception as e:
logging.error(f'插入数据出错: {e}')
logging.info("数据已成功写入数据库。")
except Exception as e:
logging.error(f'数据库连接或写入失败: {e}')